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
Bryan REVELANT 2Bryan REVELANT 2 

Apex trigger on Approval.ProcessSubmitRequest multiple user id

Hello,

3 objects.

Custom object
Corss object
User object


I want to insert a list of userid from my cross object to flow within an approval. I have built code to do this with one user or hard code user id. However I want to take the cross object user id's and insert them into setNextApproverIds. however this appears to bomb everytime I try. Any thoughts on the below




trigger ADRTrigger on ADR__c(After update)
{

//    list<User> UserList = new list<User>();
      list<id> ADRUserIDVar = new list <id>();

public void ADRTrigger(list<ADRUser__c> NewADRU, map <id, ADRUser__c> NewMapSAS){
   map <id, list <id>> ADRUserMap = new map  <id, list <id>>();             
        
        for (ADRUser__c listSAS : NewADRU){
            system.debug('Log: ' + NewADRU.SIZE());
            list<id> ADRIds1st = new list<id>();
            if(ADRUserMap.containsKey(listSAS.id)){
                ADRIds1st = ADRUserMap.get(listSAS.id);
            }          
            ADRIds1st.add(listSAS.id);
            ADRUserMap.put(listSAS.User__c, ADRIds1st);         
            ADRUserIDVar.add(listSAS.User__c);               
        }
        system.debug('Variable ADRUserIDVar: ' + ADRUserIDVar);   
        }
      
      
    for (Integer i = 0; i < Trigger.new.size(); i++)
    {
     try
     {

        if(Trigger.new[i].Next_Step__c == 'Submit' && Trigger.old[i].Next_Step__c != 'Submit')
        {
           submitForApproval(Trigger.new[i]);
        }
     }catch(Exception e)
     {
         Trigger.new[i].addError(e.getMessage());
     }
    }

    //Methods to Submit the record
    public void submitForApproval(ADR__c opp)
    {
        // Create an approval request for the ADR__c
        Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
        req1.setComments('Submitting request for approval automatically using Trigger');
        req1.setObjectId(opp.id);
        req1.setNextApproverIds(ADRUserIDVar);

        Approval.ProcessResult result = Approval.process(req1);
    }
    //Get ProcessInstanceWorkItemId using SOQL
    public Id getWorkItemId(Id targetObjectId)
    {
        Id retVal = null;

        for(ProcessInstanceWorkitem workItem  : [Select p.Id from ProcessInstanceWorkitem p
            where p.ProcessInstance.TargetObjectId =: targetObjectId])
        {
            retVal  =  workItem.Id;
        }

        return retVal;
    }
}
SFDC_DevloperSFDC_Devloper
ShashForceShashForce
Hi Bryan,

You cannot add a list of users as next approvers in APEX, as it takes only a single ID. However, please check if this helps: http://salesforce.stackexchange.com/questions/9049/apex-approval-process-that-requires-unanimous-approval-from-multiple-approvers

Thanks,
Shashank 
MagulanDuraipandianMagulanDuraipandian
Add the users to the Public Group and populate the Public Group id in next approver.

If this solves your problem, kindly mark it as the best answer.
Hit Like, if it saved your work :-)

Regards,
Magulan
http://www.infallibletechie.com
Bryan REVELANT 2Bryan REVELANT 2
The approval flow should be dynamic meaning that I will not know whom should be the approve to put in the group.