0
votes

This trigger pulls values from the Process Instance object, stores them in a list, and updates a field on the custom object. The problem is that in order to see the value, the user must update and save the record. Normally I'd add an 'After Update' trigger on the Process Instance object, but that is not allowed by Salesforce.

Any help or suggestions would be appreciated.

trigger update_Provisioner on Service_Request__c (before update) {

for(Service_Request__c sr:Trigger.new){

List <ProcessInstance> pi = [SELECT Id, CreatedDate from ProcessInstance where TargetObjectId = :sr.Id ORDER BY CreatedDate DESC limit 1];
List <ProcessInstanceStep> op = [SELECT Id, StepStatus, ActorId, OriginalActorId, CreatedDate FROM ProcessInstanceStep where ProcessInstanceId = :pi[0].Id ORDER BY CreatedDate DESC limit 1];


        if(op.size()>0){

        //System.debug('Hello'+sr.Provisioner__c+' '+op[0].StepStatus + ' '+ op[0].ActorId + ' ' +op[0].OriginalActorId + ' '+ op[0].CreatedDate);
        sr.Provisioner__c = op[0].ActorId;

          }       

}

}

1
Rather than doing this in a trigger, have you tried adding a field update directly in the Approval Process that's initiating this? - amrcn_werewolf

1 Answers

0
votes

Change update_Provisioner trigger to After Update event. Update again trigger.new list, and stop the loop by static flag.

Something like this:

trigger update_Provisioner on Service_Request__c (after update) {
    if(flag)
      return;
    flag=true;
    for(Service_Request__c sr:Trigger.new){

List <ProcessInstance> pi = [SELECT Id, CreatedDate from ProcessInstance where TargetObjectId = :sr.Id ORDER BY CreatedDate DESC limit 1];
List <ProcessInstanceStep> op = [SELECT Id, StepStatus, ActorId, OriginalActorId, CreatedDate FROM ProcessInstanceStep where ProcessInstanceId = :pi[0].Id ORDER BY CreatedDate DESC limit 1];


        if(op.size()>0){

        //System.debug('Hello'+sr.Provisioner__c+' '+op[0].StepStatus + ' '+ op[0].ActorId + ' ' +op[0].OriginalActorId + ' '+ op[0].CreatedDate);
        sr.Provisioner__c = op[0].ActorId;

          }       

}
update trigger.new;
}