function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
prs8778prs8778 

How to Include Approver's Comments In the Email

Hi,

 

As part of the approval workflow in salesforce, when a request is either approver or rejected, an email goes back to the submitter notifying that his/her request has been approved and/or rejected. Is there a way to include Approver's comments in that email so that Submitter knows the reason why it was approved/rejected? It seems simple enought to just pull Approver's Comments field on the email however we've been told that it isn't easy to accomplish. If anyone has done this before, I would appreciate if you could share your knowledge on this matter. Thanks.

agum34agum34

We were able to do this by using a custom component in the visualforce email template.  This allows you to use a controller to query for data that isn't directly available in the visualforce email template.  Also allows you to sort the data. Note this is the full list of all approvers including the person who submitted the record. 

 

Visualforce email template

 

<p><b>Approval History</b>
<c:QuoteApprovalHistory quoteId="{!relatedTo.ID}"/>
</p>

 

 

Approval History component

 

<apex:component controller="QuoteApprovalHistoryController" access="global">
    <apex:attribute name="quoteId" assignTo="{!quoteId}" type="String" description="Id of the quote"/>  
    <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>

 

 

Controller

 

public class QuoteApprovalHistoryController {
    public String quoteId {get;set;}
    public List<ProcessInstanceHistory> getApprovalSteps() {
      if (quoteId != null) {
        Quote__c quote = [Select Id, (Select TargetObjectId, SystemModstamp, StepStatus, RemindersSent, ProcessInstanceId, OriginalActorId, IsPending, IsDeleted, Id, CreatedDate, CreatedById, Comments, ActorId From ProcessSteps order by SystemModstamp desc) from Quote__c where Id = :quoteID];
        return quote.ProcessSteps;
      }
      return new List<ProcessInstanceHistory> ();
    }  
    
}

 

 

You may want to add the following style (or something better) to your email template as well. 

 

<style type="text/css">
                body
                {
                    font-family: Verdana;
                    font-size:12px;
                }
                
                table 
                {
                    border-width: 1px;
                    border-spacing: 0px;
                    border-style: solid;
                    border-color: #000000;
                    font-family: Verdana;
                    font-size:12px;
                }
            
                td
                {
                    padding: 1px;
                    border-width: 1px;
                    border-spacing: 0px;  
                    border-style: solid;
                    border-color: #000000;
                }
                
                th
                {
                    padding: 1px;
                    border-width: 1px;
                    border-spacing: 0px;  
                }

            </style>

 

 

 

GasparGaspar

Getting this error message: Error occurred trying to load the template for sending preview email: List has no rows for assignment to SObject. Please try editing your markup to correct the problem.

 

Can you please help?

agum34agum34

This sounds like your SOQL did not return any rows.  You can catch that exception by adding a try catch block around the SOQL query.

http://corycowgill.blogspot.com/2011/02/infamous-rookie-error.html

 

public class QuoteApprovalHistoryController {
    public String quoteId {get;set;}
    public List<ProcessInstanceHistory> getApprovalSteps() {
      if (quoteId != null) {
try { Quote__c quote = [Select Id, (Select TargetObjectId, SystemModstamp, StepStatus, RemindersSent, ProcessInstanceId, OriginalActorId, IsPending, IsDeleted, Id, CreatedDate, CreatedById, Comments, ActorId From ProcessSteps order by SystemModstamp desc) from Quote__c where Id = :quoteID];
return quote.ProcessSteps;
} catch (Exception e) {
return new List<ProcessInstanceHistory>();
} } return new List<ProcessInstanceHistory> (); } }
hlisterhlister

Hi!

I've incorporated this code into approval processes we have around change control, and I love that you have found a way to display the approval history in email. This works great for the initial request email to the approver, but I'm also using it in the approval steps to notify the submitter once approved or rejected. Things seem to be off when the visualforce template is used this way, as if it's a step behind and the approval updates aren't shown in the email until the next person approves.

 

For example, when Approver #2 approves the change, the email is sent to the submitter, but the approval history shows the step as Pending for Approver #2 and does not include their comments. Only when Approver #3 approves the request does the email show that Approver #2 approved it with comments, but does not show approval from Approver #3. It shows Approver #3 as Pending.

 

Please let me know if you have suggestions on how to have the code pull the most recent approval information and include it in the email for each step as it is approved or rejected. My code is structurally exactly like what you've posted previously, but if you need to see it, I can post it here.

 

Thanks in advance for the help!

 

agum34agum34
I think this is because the email goes out before the approval process is fully committed to the database. You might try to see if you can get the current approver comment another way. I believe with a standard email template there is a merge field for the current approvers comment but I don't think you can access that merge field from the VisualForce template. If I can find anything I'll post it in the next few days.