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
venkat 190venkat 190 

how to display approval comments using vf component on vf email template?

Hi ,
I want to display approval (approved or rejected or submitted ) comments in VF email template, I have creted VF component and apex class, it is working fine for normal approval process but in case of unanimuous approval process,comments are displaying as previus comments like while submiting time displaying as null,if first approver approved and it went for next approver this time it is displaying submitter comments, So please help me how to resolve this one?
please find my VF component and apex class.
VF Component:
<apex:component controller="ApprovalRequestController" access="global">
    <apex:attribute name="relatedToId" assignTo="{!targetObjectId}" type="String" description="ID of the record whose last approval comments to retrieve"/>
    <apex:outputText value="{!comments}"/>
</apex:component>

Apex class:
public without sharing class ApprovalRequestController {

    // ID of the record whose most recent approval process comments to retrieve
    public ID targetObjectId { get; set; }
    
    // The most recent approval process comments
    // Could show in visualforce email template, for example
    public String comments {
        get {
            if ( comments == null ) {
                ProcessInstanceStep lastStep = getLastApprovalStep();
                comments = ( lastStep != null ) ? lastStep.comments : '';
            }
            return comments;
        }
        private set;
    }
    public String status {
        get {
            if ( status == null ) {
                ProcessInstanceStep lastStep = getLastApprovalStep();
                status = ( lastStep != null ) ? lastStep.StepStatus : '';
            }
            If(status == 'Started') status ='Submitted';
            return status;
        }
        private set;
    }
    public String approver {
        get {
            if ( approver == null ) {
                ProcessInstanceStep lastStep = getLastApprovalStep();
                approver = ( lastStep != null ) ? lastStep.OriginalActor.Name : '';
            }
            return approver;
        }
        private set;
    }
    
    public ApprovalRequestController() {}
    
    // Queries the most recent approval process step for the target record
    private ProcessInstanceStep getLastApprovalStep() {
        List<ProcessInstanceStep> steps = new List<ProcessInstanceStep>([
            SELECT
                Comments,ActorId,StepStatus,OriginalActor.Name
            FROM
                ProcessInstanceStep
            WHERE
                ProcessInstance.TargetObjectId = :targetObjectId
            ORDER BY
                SystemModStamp DESC
            LIMIT
                1
        ]);
        return ( steps.size() > 0 ) ? steps[0] : null;
    }
    
}
 
VinayVinay (Salesforce Developers) 
Hi Venkat,

Check below examples that can help you.

https://salesforce.stackexchange.com/questions/295815/send-approval-comments-in-visualforce-email
https://gist.github.com/douglascayers/46bb1e92be9909d60fee

Thanks,