0

So basically I want to get the comments when pending approval is being clicked if it's populated by the submitter and send a visual force email template.

Sample code below for reference. Disregard the architecture as I'm still in the process of testing how it works.

trigger testTrigger on Quote (after update) {
    if(Trigger.isAfter){
        if(Trigger.isUpdate){
            List<Quote> quoteRec = Trigger.new;
            List<ProcessInstanceStep> instancesSteps2 = [select Comments,StepStatus,ProcessInstanceId from ProcessInstanceStep where ProcessInstance.TargetObjectId =: quoteRec[0].Id];
            System.debug('instancesSteps : ' + instancesSteps2); // NULL IF PENDING APPROVAL - POPULATED IF REJECTED OR APPROVED
            List<ProcessInstance> processInstance = [SELECT Status, TargetObjectId FROM ProcessInstance WHERE TargetObjectId =: quoteRec[0].Id];
            System.debug('processInstance : ' + processInstance ); // POPULATED ALL THE TIME
            
            for (ProcessInstance pi: [SELECT Id, Status, TargetObjectId, TargetObject.Name, (SELECT Id, ActorId, Actor.Name, OriginalActorId, OriginalActor.Name, StepStatus, Comments, 
            ProcessNode.Name, CreatedDate 
            FROM StepsAndWorkitems) 
            FROM ProcessInstance WHERE TargetObjectId =: quoteRec[0].Id] ) {
                System.debug('pi : ' + pi); // POPULATED ALL THE TIME
                System.debug('pi.StepsAndWorkItems : ' + pi.StepsAndWorkItems); // NULL IF PENDING APPROVAL - POPULATED IF REJECTED OR APPROVED
                
                //will not enter here since pi.StepsAndWorkItems is null
                for (ProcessInstanceHistory pih : pi.StepsAndWorkItems) {
                    System.debug('pih : ' + pih); // NULL IF PENDING APPROVAL - POPULATED IF REJECTED OR APPROVED
                     System.debug('pih.Comments : ' + pih.Comments); // NULL IF PENDING APPROVAL - POPULATED IF REJECTED OR APPROVED
                }          
            }
        }
    }
}

based on the documentation ProcessInstanceHistory 'This read-only object shows all steps and pending approval requests associated with an approval process' -

https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_processinstancehistory.htm

how come during the initial phase of the approval process (which is when submit for approval is clicked) I'm not getting any StepsAndWorkItems record.

The weird thing is after I approved/reject the record, I can see the comments of pending approval.

1 Answer 1

1

for future reference, I was able to solve this by using @future

I think the behavior would be that ProcessInstanceHistory will only be created after commit of all records during submit for approval.

Thanks to Krista Kelly (https://developer.salesforce.com/forums/?id=9062I000000DHddQAG). I tried hardcoding the id in my vf email template -> record submit for approval -> Comments were being populated in the template when I test it.

Next plan would be to just send the vf email template using apex.

1
  • Welcome to Salesforce SE! Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
    – identigral
    May 6 at 17:05

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .