0

I am trying to send an email with CC when an approver approved a quote in approval process. For this in approval steps I make Current Approver field update and I have created trigger on these updates. It seems to work ok and my trigger sends Emails with cc, but I faced with a problem that I need to use information from Process Instance Workitem in my email, but the trigger works when work item wasn't created yet! So my trigger react on field update and only after this next Process Instance Workitem is created. Any ideas how can I use information from Process Instance Workitem in my email? Here is my Approval process : enter image description here

Thanks

1
  • If i understood it correctly , you are trying to use information updated via approval process. I would suggest call a future method on meeting certain criteria then query your work item
    – Elijah
    Jan 11, 2022 at 11:27

1 Answer 1

0

I would suggest below steps to resolve your problem :

  1. Filter your records on basis of criteria
  2. Create a future method which will take list of Id of those records.
  3. Call this future method in trigger with filtered records.

Sample code :

Trigger Handler Code

List<Id> recordsId = new List<Id>();
    for(TestObject__c q : Trigger.new){
       if(q.status__c == 'Approved'){
        recordsId.add(q.Id);
        }

    }
SomeClass.getComments(recordsId);

Future Method

@future
    public static void getComments(List < id > incomingRecords) {
        
        // It will contain all the targetParent record
        List < Id > parentRecord = new List < Id > ();
        for (ProcessInstanceStep step: [SELECT Id, ProcessInstanceId, ProcessInstance.TargetObjectId, ProcessInstance.TargetObject.Type, StepStatus, Comments FROM ProcessInstanceStep WHERE ProcessInstanceId IN(SELECT Id FROM ProcessInstance WHERE TargetObjectId IN: incomingRecords)
                and stepStatus != 'Started'
                order by createdDate desc
            ]) {
            // this check is to ensure we only take latest entry for one particular object at a time
            // as multiple step entries can be present for starting , approved or rejection 
            if (!parentRecord.contains(step.ProcessInstance.TargetObjectId)) {
                
                
                // Do what you want to do
                parentRecord.add(step.ProcessInstance.TargetObjectId);
            }
        }
        
    }

You must log in to answer this question.

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