I have created an Approval Process and am launching it from an Apex code behind a custom button. This is working fine, but the problem I am facing is that I am not able to set the approver from the Apex code. It always takes the Approver that is set in the Approval Process.
Here is my code:
Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
req.setComments('Submitted for approval. Please approve. Source:ApexClass');
req.setObjectId(dcr.Id);
req.setNextApproverIds(new Id[] {'0057E000001dn5T'}); // This is the id of a user
Approval.ProcessResult result = Approval.process(req);
This submits the record for approval, but sends it to the Approver that is set in the Approval Process, and not to the user with id '0057E000001dn5T' that is set in the code
I also tried by changing the following line from above code
req.setNextApproverIds(new Id[] {'0057E000001dn5T'});
to
List<Id> approverIdList = new List<Id>();
Id approverId = [SELECT DCR_Send_to_Approver_AGN__c FROM User
WHERE Id = :UserInfo.getUserId()].DCR_Send_to_Approver_AGN__c;
approverIdList.add(approverId);
req.setNextApproverIds(approverIdList);
and
list<string> approverIdList = new list<string>();
group g = [SELECT (select userOrGroupId from groupMembers) FROM group
WHERE name= 'testUserGroup'];
for (GroupMember gm : g.groupMembers) {
approverIdList.add(gm.userOrGroupId);
}
req.setNextApproverIds(approverIdList);
...but nothing worked.
So how I can set the approver from the Apex code? Any help would be appreciated.