+ Start a Discussion
Dean Rourk 2Dean Rourk 2 

apex trigger for approval process

I keep getting a compile error: Error: Compile Error: Method does not exist or incorrect signature: [Approval.ProcessSubmitRequest].setNextApproverIds(Id) 
Eventually I will not hard code the user name 'john Taylar' and get it from the object.  

Has anyone been able to get this to work.


trigger SDRStartApprovalProcess on SDR__c (after update) {
      for( id aId : Trigger.newMap.keySet() )
{
  if( Trigger.oldMap.get( aId ).Account_Owner_id__c!= Trigger.newMap.get( aId ).Account_Owner_id__c )

     // do something here because your field has changed
        List<User> users = [select Name from User where Name= 'John Taylor'];

         id approver= users.get(0).Id ;  
         //Approver =  userMap(Trigger.newMap.get( aId ).Account_Owner__c).Id        
        
         Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitRequest();
         app.setObjectId(aid);
         app.setNextApproverIds(approver);
         Approval.ProcessResult result = Approval.process(app); 
}
 
}

}
Subhash GarhwalSubhash Garhwal
Hi Dean Rourk,

You need to pass list of users in setNextApproverIds.

Like this 

trigger SDRStartApprovalProcess on Account (after update) {

//Do something here because your field has changed
List<User> users = [select Name from User where Name = 'John Taylor'];

List<Id> approvers = new List<Id>();
approvers.add(users[0].Id);

//Check for List size
if(approvers.size() > 0) {

  for( id aId : Trigger.newMap.keySet()) {

   if( Trigger.oldMap.get( aId ).Account_Owner_id__c != Trigger.newMap.get( aId ).Account_Owner_id__c) {
   
  
     Approval.ProcessSubmitRequest app = new Approval.ProcessSubmitRequest();
     app.setObjectId(aid);
     app.setNextApproverIds(approvers);
     Approval.ProcessResult result = Approval.process(app);
   }

  }

}
}

one more thing this code only works if you have any Applicable Approval Process.

Regards
Subhash