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' -
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.