2

I have an apex class that submits opportunities for approval if they are over a certain threshold. This works great when processing small batches, but when trying to submit larger batches it fails with "Too many DML Statements".

I haven't been able to find anything that says I can process a list of records with the Approval.ProcessSubmitRequest class - so am doing it within a for loop. I tried swapping to a @future class for the Approval, but that has a limit of 50 @future calls as well.

Looking to bulkify this or identify an alternative option.

for(Opportunity o :lstOppty){
    String recordtypename = Schema.SObjectType.Opportunity.getRecordTypeInfosById().get(o.recordtypeid).getname();
    if(o.BSO_Audit_Flag__c && o.Last_Review_Date__c == null && opptyRTaudit.contains(recordtypename) && Approval.isLocked(o.id) == false){
        Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
        req.setComments('Opportunity was submitted for approval.');
        req.setObjectId(o.Id);
        try{
            Approval.ProcessResult result = Approval.process(req);
            System.debug('Submitted for approval successfully: '+result.isSuccess());
        }
        catch(Exception e) {
            System.debug('Submitted for approval failed. This usually means the BSO Alignment or Country on User profile is not set correctly. Error Message: = ' + e.getMessage());
        }
    }
}
4

Note that the Approval.process method has a signature which accepts a List<Approval.ProcessRequest>:

public static Approval.ProcessResult [] process(Approval.ProcessRequest[] approvalRequests)

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.