How to set the approvers for all steps in an approval process

apex-codesalesforce

I have an approval process with three steps, all of which are set to Assigned Approver = Manually Chosen. When the user submits the record for approval, I'd like to have Apex code determine who the three approvers are. However, I don't see a way to hook into the approve request submission.

If I submit the approval with Apex Using Approval.process(), I can set the initial (and only the initial) approver with ProcessSubmitRequest.setNextApproverIds(). This call leads you to believe you can specify multiple approvers since it takes an array of Ids, but the array can only have 1 element, or else runtime a error occurs.

Once I know what the first approver's response is, I can use Apex to submit her response and, again, set the immediately next approver by passing a ProcessWorkitemRequest instance to Approval.process(). An important note here is that the approver must not approve via the standard UI. Instead, they must do something that invokes the Apex code so that we can set who the next approve should be. A trigger on the object under review, or a custom button + VF page could be used to invoke the Apex.

My main question is, how can I make sure that the user does not use the standard approval buttons? They appear in the Approvals related list and on the salesforce home screen. It may be in other places as well. Again, if they use the standard submit and approve buttons, I don't have any way to hook in to set the next approver.

Best Answer

We ran into a similar issue a while back and solved it by creating custom lookup fields to certain users. For example, if we wanted to route an approval request up to a Director and then a VP, we added Director__c and MarketVP__c fields to the object. These fields were populated in code by climbing the role hierarchy whenever a request was submitted. Our approval process's steps then chose who the assignee would be based on the values in these fields (first step would be assigned to Related User: Director and the second step would be assigned to Related User: Market VP, etc.).

To get around the standard approval button issue (we had other reasons for hiding it), we just hid that from their homepage layouts and built our own VF page and included it in a custom homepage component. This component functioned as an inbox with links to any records that were pending the user's approval. All user interaction with the approval objects was handled through other VF pages with their own Approve and Reject buttons. I don't know if the objects you're submitting to the approval process even use VF pages, so this may not be feasible for your situation.

A lot of customization for something that shouldn't need it, I know. Might not be the answer you're looking for, but hopefully it's some food for thought.

Related Topic