2
trigger CaseApprover1 on Case (after update) {
    List<ProcessInstance> instances = [SELECT Id,TargetObjectId,(SELECT Id, StepStatus, Comments FROM Steps) 
                                       FROM ProcessInstance Where ProcessDefinitionId =:'04a7F000000HMpZQAW'];
    system.debug('===>'+instances);
    Set<String> objectIds = new Set<String>();
    for(ProcessInstance pi:instances){
        objectIds.add(pi.TargetobjectId);
    }
    Map<Id,Case> csemap = new Map<Id,Case>([Select Comments__c from Case Where Id in:objectIds ]);
    system.debug('=====>'+csemap);
    for(ProcessInstance pi:instances){
        for (ProcessInstanceStep step : pi.Steps) {
            if(step.StepStatus == 'Approved') {
                csemap.get(pi.TargetObjectId).Comments__c = step.Comments;
            }
            else if(step.StepStatus == 'Rejected'){
                csemap.get(pi.TargetObjectId).Comments__c = step.Comments;
            }
        }
    }
    update csemap.values();
}
0

You are updating Case in update csemap.values(); which is again trigging the CaseApprover1 trigger.

And in the trigger Case is getting updated again.

This becomes endless.

You need to implement a mechanism to break this endless process.

You can you a static variable as a flag which can be set and you should check value for the same before calling update csemap.values();.

And another way

You can achieve this functionality by creating trigger for ProcessInstance and update the Case object there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.