Apex approval process that requires unanimous approval from multiple approvers

Once every few months this requirement appears as part of a project. Here is some sample code:

trigger AfterUpdate on Contact (After update) {

    for(Contact newContact: Trigger.new){

        Contact oldContact = Trigger.oldMap.get(newContact.id);

        if (oldContact.mailingStreet <> newContact.mailingStreet) {
            Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();

            req.setComments('Approving an address change.');
            req.setObjectId(newContact.id);
            req.setNextApproverIds(new List<Id>{'005i0000000h5az', '005i0000000hASk'});

            Approval.ProcessResult res = Approval.process(req);
        }
    }
}

This yields the following error message:

Error:Apex trigger AfterUpdate caused an unexpected exception, contact your administrator: AfterUpdate: execution of AfterUpdate caused by: System.DmlException: Process failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, missing required field: []: Trigger.AfterUpdate: line 14, column 1

If you change the list in the setNextApproverIds method parameter to only have a single ID then this error disappears.

This begs the question, can you require multiple approvers through Apex? And if so, how?

For added context, there is only one approval step and the details are:

Approval Step Details

Answer

Might this statement in the docs for the ProcessRequest class, the parent class for the ProcessSubmitRequest, provide some insight?

If the next step in your approval process is another Apex approval process, you specify exactly one user ID as the next approver.

ProcessRequest class


Update:

One method to do this would be to rearrange the trigger behavior to create a single process request per approver and then use the process method overload which takes a list parameter.

trigger AfterUpdate on Contact (After update) {

    for(Contact newContact: Trigger.new){

        Contact oldContact = Trigger.oldMap.get(newContact.id);

        if (oldContact.mailingStreet <> newContact.mailingStreet) {
            List<Id> approverIds = new List<Id>{'005i0000000h5az', '005i0000000hASk'};
            List<Approval.ProcessSubmitRequest> requests = new List<Approval.ProcessSubmitRequest>();

            for (Id approverId : approverIds) {
                Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();

                req.setComments('Approving an address change.');
                req.setObjectId(newContact.id);
                req.setNextApproverIds(new List<Id>{approverId});

                requests.add(req);
            }

            List<Approval.ProcessResult> results = Approval.process(requests);
        }
    }
}

Would this suffice for your scenario?

Attribution
Source : Link , Question Author : Wes Nolte , Answer Author : Wes Nolte

Leave a Comment