Monday, January 17, 2011

Master Detail relationship, 2 master for 1 child

When there will be two master object relationship for 1 child object.

Suppose one child record is added and there are two master records associated with it. If one master record also gets deleted then child record gets deleted as well.

For eg.

Child object A
Master Object B and C

Child record A1
Master Records are B1 and C1

if record B1 is deleted then record A1 also gets deleted irrespective of record C1 as master is still exists.

Capture Approval/Rejection Comments in email

First we will need to create a component which can be used in VF email template. Pass on your record Id to this component. When opportunity will be rejected assign the VF email template to rejection action.
Below is the component and VF template when opportunity record is rejected.

Component:
<apex:component controller="OpportunityApprovalHistoryController" access="global">
    <apex:attribute name="opptyId" assignTo="{!opptyId}" type="String" description="Id of the opportunity"/> 
    <apex:dataTable value="{!approvalSteps}" var="step">
        <apex:column value="{!step.SystemModstamp}" headerValue="Date"/>
        <apex:column value="{!step.StepStatus}" headerValue="Status"/>
        <apex:column value="{!step.OriginalActorId}" headerValue="Assigned To"/>
        <apex:column value="{!step.ActorID}" headerValue="Actual Approver"/>
        <apex:column value="{!step.Comments}" headerValue="Comments"/>
    </apex:dataTable>
</apex:component>
Component Controller:
public class OpportunityApprovalHistoryController {
    public String opptyId {get;set;}
    public List<ProcessInstanceHistory> getApprovalSteps() {
      if (opptyId != null) {
        Opportunity quote = [Select Id, (Select TargetObjectId, SystemModstamp, StepStatus, RemindersSent, ProcessInstanceId, OriginalActorId, IsPending, IsDeleted, Id, CreatedDate, CreatedById, Comments, ActorId From ProcessSteps order by SystemModstamp desc) from Opportunity where Id = :opptyId];
        return quote.ProcessSteps;
      }
      return new List<ProcessInstanceHistory> ();
    } 

}
Visualforce Email Template:
<messaging:emailTemplate subject="Your Opportunity is Rejected" recipientType="User" relatedToType="Opportunity">
<messaging:HtmlEmailBody >
Rejected!
Your Opportunity has been rejected.
Below are the approval rejection comments
<html>
<head>
    <meta content="text/html;charset=utf-8" http-equiv="Content-Type"/>
    <meta name="Template" content="Response"/>
</head>
<body>
<p><b>Approval History</b>
<c:OpportunityApprovalHistory opptyId="{!relatedTo.Id}"/>
</p>
</body>
</html>
</messaging:HtmlEmailBody>
</messaging:emailTemplate>