function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Geof131313Geof131313 

Batching Submit For Approval From Triggers

Hello,

I wrote a generic trigger that submits an Opportunity for approval when a picklist meets the appropriate criteria. The purpose is to allow us to submit using a workflow rule/upload etc.

 

Code is here:

trigger SubmitFromTrigger on Opportunity (after insert, after update) {

    list<Opportunity> updateOpps = new list<Opportunity>();

    for(Opportunity opp : trigger.new){
        if( opp.submit_from_trigger__c == 'Yes'){
        
            Opportunity placeHolderOpp = new Opportunity(id = opp.id, submit_from_trigger__c = 'No');
            updateOpps.add(placeHolderOpp);
            
            approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
            req1.setObjectId(opp.id);
            approval.ProcessResult result = Approval.process(req1);
        }
    }
    Update updateOpps;
       
}

 

This script works as it is supposed to. The only problem is that I could not figure out how to batch the submit for approval command, so when I ran a test in our sandbox submitting 23 at a time it worked, but when I did it using 102 it did not.

 

So I guess my questions are 1) Is it possible to batch the approval submit command, 2) If so, how?, and 3) If not, can we change that in an upcoming release?

 

Thanks,

Geoffrey

Best Answer chosen by Admin (Salesforce Developers) 
Geof131313Geof131313

Thanks for the tip. Looks like all I had to do was add "[]" to the end of the result call. Final code is here:

 

trigger SubmitFromTrigger on Opportunity (after insert, after update) {

    list<Opportunity> updateOpps = new list<Opportunity>();
    list<Approval.ProcessSubmitRequest> reqList = new list<Approval.ProcessSubmitRequest>();

    for(Opportunity opp : trigger.new){
        if( opp.submit_from_trigger__c == 'Yes'){        
            Opportunity placeHolderOpp = new Opportunity(id = opp.id, submit_from_trigger__c = 'No');
            updateOpps.add(placeHolderOpp);
            approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
            req1.setObjectId(opp.id);
            reqList.add(req1);
        }
    }


    approval.ProcessResult[] result = Approval.process(reqList);
    Update updateOpps;       
}

All Answers

bob_buzzardbob_buzzard
According to the Apex Code Developer's Guide, there is a variant of Approval.process that takes a list of Approval.ProcessRequest objects rather than a single instance.
Geof131313Geof131313

Thanks for the tip. Looks like all I had to do was add "[]" to the end of the result call. Final code is here:

 

trigger SubmitFromTrigger on Opportunity (after insert, after update) {

    list<Opportunity> updateOpps = new list<Opportunity>();
    list<Approval.ProcessSubmitRequest> reqList = new list<Approval.ProcessSubmitRequest>();

    for(Opportunity opp : trigger.new){
        if( opp.submit_from_trigger__c == 'Yes'){        
            Opportunity placeHolderOpp = new Opportunity(id = opp.id, submit_from_trigger__c = 'No');
            updateOpps.add(placeHolderOpp);
            approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
            req1.setObjectId(opp.id);
            reqList.add(req1);
        }
    }


    approval.ProcessResult[] result = Approval.process(reqList);
    Update updateOpps;       
}

This was selected as the best answer