Monday, January 17, 2011

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>

7 comments:

  1. Works great, thanks! For completeness, can you provide test code?

    ReplyDelete
  2. I wonder why other professionals don’t notice your website much m glad I found this.
    article marketing

    ReplyDelete
  3. Awesome! thanks so much for the instructions. This made my day!

    ReplyDelete
  4. Infact there is another shortcut to get approval history without using custom component and controller.

    In VF email template, approval steps can be accessed as related list. Here is sample code(I have removed html tags, you have to add tags).
    -----------------------------
    apex:repeat var="cx" value="{!relatedTo.ProcessSteps}"
    apex:outputPanel rendered="{!cx.StepStatus=='Approved'}"
    p Date: {cx.createdDate}
    Status: {!cx.StepStatus}
    Approver: {!cx.Actor.Name}
    Comments: {!cx.comments} p
    apex:outputPanel apex:repeat
    --------------------------------

    ReplyDelete
  5. Awesome blog, thanks so much for the instructions. Salesforce can help you get the most out of the platform.Training is required for someone to gets introduced to a new job or roll out a business change.For more details salesforce training hyderabad

    ReplyDelete
  6. hi maybe im wrong but plz help...when i am quering the information about the approval i am not getting the current one.. i am only getting the history information..plz if anyone know do help

    ReplyDelete