Print this page

Recalled Approval Process Comments in email template missing

Knowledge Article Number 000176589
Description In some instance, you may wish to send an email to a specified list of users with the contents of an Approval History (Approved/Rejected/Recalled comments/approvers/ etc) through a VF email template.

The below setup will work but as the Recalling of an approval process is not like a normal Process Instance, the comments from a Recall are not committed until after the Recall Actions from an approval process complete. This results in the email being delivered without the latest recall comment in the email. In the example below, user has submitted and approved. Then user resubmitted and recalled but the comments are missing for recall and it is not captured along with the Status of recall.

Recalled! Your Opportunity has been recalled. Below are the approval history comments
Approval History
DateStatusAssigned ToActual ApproverComments
09/04/2013 11:50PendingDarren BolandDarren Boland 
09/04/2013 11:50SubmittedDarren BolandDarren Boland 
09/04/2013 11:50ApprovedDarren BolandDarren Bolandtesting approval
09/04/2013 11:49SubmittedDarren BolandDarren Boland 
Resolution First we will need to create a component which can be used in VF email template. Pass on your record Id to this component which will then use it within its controller to retrieve the approval steps. You can then use the VF Email template within the approval process as email alerts on Approval or Rejection.

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 Recalled" recipientType="User" relatedToType="Opportunity">
<messaging:HtmlEmailBody >
Recalled!
Your Opportunity has been recalled.
Below are the approval history 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>


The above setup will get you as far as working for the Approved and Rejected comments. To include the Recalled comments you must complete the following:
1) On your Opportunity object, create a hidden checkbox so your users do not see it as they should not modify it. Call it something like RecallCommentCheck(DoNotDelete)
2) Go to your Approval Process and remove the Email Alert from the Recall Actions section and add a new field update to update the checkbox above. Make sure you check the "Re-evaluate Workflow Rules after Field Change" checkbox on this field update.
3) Create a new workflow rule on your Opportunity object.
i) Set the rule criteria to Checkbox above - equals - True
ii) Set the evaluation too: "created, and every time it’s edited"
iii) Add an Email Alert with the recall template
iv) Add a field update to uncheck the above checkbox.

For the following scenario (Submitted for approval, Approve record, submit for approval, Reject, submit for approval, Recall) the email turned out like so:

Recalled! Your Opportunity has been recalled. Below are the approval history comments
Approval History
DateStatusAssigned ToActual ApproverComments
09/04/2013 11:52RecalledDarren BolandDarren Bolandrecall comment
09/04/2013 11:51SubmittedDarren BolandDarren Boland 
09/04/2013 11:51RejectedDarren BolandDarren Bolandrejection comment
09/04/2013 11:50SubmittedDarren BolandDarren Boland 
09/04/2013 11:50ApprovedDarren BolandDarren Bolandapprove comment
09/04/2013 11:49SubmittedDarren BolandDarren Boland 




promote demote