I've got some records that were accidentally entered into an approval process that I want to remove - I keep running into governor limits when doing so. The approach I'm taking currently doesn't feel right, so I'm wondering if there is an easier way to get the process work items of records currently in an approval flow.
List<Approval.ProcessWorkitemRequest> requests = new List<Approval.ProcessWorkitemRequest> ();
// get all records that need to be removed from approval process
for(Opportunity o : [Select Id from Opportunity where StageName = 'Lost' and Deal_Number__c like 'SLE%']){
// find the process instances
List<ProcessInstance> p = [Select Id from ProcessInstance where TargetObjectId = :o.Id];
if(p.size() > 0){
// if record has process instance, find processInstanceWorkItems
List<ProcessInstanceWorkItem> pp = [Select Id from ProcessInstanceWorkItem where ProcessInstanceId = :p[0].Id];
if(pp.size() > 0){
// if there's a work item, set the action to 'removed' and execute
Approval.ProcessWorkitemRequest req2 = new Approval.ProcessWorkitemRequest();
req2.setAction('Removed');
req2.setWorkitemId(pp[0].Id);
requests.add(req2);
}
}
}
Approval.ProcessResult[] processResults = null;
processResults = Approval.process(requests, true);
Approval.process
? Also, can you post any limits errors you are getting? – Caspar Harmer Dec 21 '15 at 17:03