I have a approval process which is triggering when status of record is pending review. I have created a workflow rule to change the status to expired when the record is passed 14 days from its creation. The workflow is firing and status is changed to expired. But the record is still in approval process and if i approve it is again changed to approved. I am trying to unlock the record when status is changed to expired in trigger handler in before update method. I even tried to remove the process item in after update. Still nothing happened. Should i use before or after update in trigger? Is there anyway i can achieve this.
1 Answer
You should use After Update , here's the sample code
List<Approval.ProcessWorkitemRequest> processRequestList = new List<Approval.ProcessWorkitemRequest>();
for(ProcessInstanceWorkItem pIWorkItem : processInstanceWorkItemList){
Approval.ProcessWorkitemRequest workItemRequest = new Approval.ProcessWorkitemRequest();
workItemRequest.setAction('Removed');
workItemRequest.setWorkitemId(pIWorkItem.Id);
processRequestList.add(workItemRequest);
}
if(!processRequestList.isEmpty()){
List<Approval.ProcessResult> processResults = Approval.process(processRequestList);
}