2

I have an idea of approving/rejecting multiple records via apex. I read from https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_process_example.htm and I was able to approve/reject many records at a time.

My problem is that I used for loop for approving all of the records, which caused an error of Too many SOQL queries (which happened because each record(Account) is connected to 2 triggers and I used like 100 records for testing)and having the process caused the triggers to run once for every record.

Is there a way to approve/reject multiple ProcessWorkitemRequests at once? (like maybe putting them first in a list before using DML statements)

Here is my code:

Set<Id> accIds = (new Map<Id, Account>([SELECT Id FROM Account])).keySet();
Set<Id> pIds = (new Map<Id, ProcessInstance>([SELECT Id,Status,TargetObjectId FROM ProcessInstance where Status='Pending' and TargetObjectId in :accIds])).keySet();
Set<Id> pInstanceWorkitems = (new Map<Id, ProcessInstanceWorkitem>([SELECT Id,ProcessInstanceId FROM ProcessInstanceWorkitem WHERE ProcessInstanceId in :pIds])).keySet();

for (Id pInstanceWorkitemsId:pInstanceWorkitems){
    system.debug(pInstanceWorkitemsId);
        Approval.ProcessWorkitemRequest req2 = new Approval.ProcessWorkitemRequest();
        req2.setComments('mRequest.');
        req2.setAction('Reject');
        req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});

        // Use the ID from the newly created item to specify the item to be worked
        req2.setWorkitemId(pInstanceWorkitemsId);

        // Submit the request for approval
        Approval.ProcessResult result2 =  Approval.process(req2);
}
4
  • why don't you use list of Approval.ProcessWorkitemRequest ? then you can use that list outside for loop for submit
    – Ratan Paul
    Commented Apr 29, 2016 at 8:27
  • I already tried that but I get 'Illegal assignment from List<Approval.ProcessResult> to Approval.ProcessResult'. When I changed into 'List<Approval.ProcessResult> result2 = Approval.process(req2);' I get "Not Serializable: com/salesforce/api/fast/List$$lcom/salesforce/api/Approval/ProcessWorkitemRequest$$r"
    – Micz Domz
    Commented Apr 29, 2016 at 8:47
  • 2
    few days back this is what I tried.... Approval.ProcessSubmitRequest[] lRequests = New Approval.ProcessSubmitRequest[]{}; for(Project_Coverage_Team_Member__c objPCTMember: lstProCovTeamMemberToInsert) { Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest(); req1.setComments('Submitting request for approval.'); req1.setObjectId(objPCTMember.Id); lRequests.add(req1); } Approval.ProcessResult[] results =Approval.process(lRequests); try same like for your case
    – Ratan Paul
    Commented Apr 29, 2016 at 8:50
  • 2
    Thanks for the idea...I already solved this problem using your concept!!!
    – Micz Domz
    Commented Apr 29, 2016 at 9:33

1 Answer 1

7

I finally got it. Instead of approving each record one at a time, I realized I can put all requests in a list before approving them all at once.

Set<Id> accIds = (new Map<Id, Account>([SELECT Id FROM Account])).keySet();
Set<Id> pIds = (new Map<Id, ProcessInstance>([SELECT Id,Status,TargetObjectId FROM ProcessInstance where Status='Pending' and TargetObjectId in :accIds])).keySet();
Set<Id> pInstanceWorkitems = (new Map<Id, ProcessInstanceWorkitem>([SELECT Id,ProcessInstanceId FROM ProcessInstanceWorkitem WHERE ProcessInstanceId in :pIds])).keySet();

List<Approval.ProcessWorkitemRequest> allReq = new List<Approval.ProcessWorkitemRequest>(); 
for (Id pInstanceWorkitemsId:pInstanceWorkitems){
    system.debug(pInstanceWorkitemsId);
        Approval.ProcessWorkitemRequest req2 = new Approval.ProcessWorkitemRequest();
        req2.setComments('mRequest.');
        req2.setAction('Reject'); //to approve use 'Approve'
        req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});

        // Use the ID from the newly created item to specify the item to be worked
        req2.setWorkitemId(pInstanceWorkitemsId);

        // Add the request for approval
        allReq.add(req2);
}
Approval.ProcessResult[] result2 =  Approval.process(allReq);

You must log in to answer this question.

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