Tuesday, 2 February 2016

How to Invoke approval process from a custom button

In some scenario we have to decided approver as dynamically. Say who will be the first approver and who is the second. 

In that case create create lookup field (First_Approver__c, Second_Approver__c)to user as approver and those field will be used on creating approval steps of approval process . 

Aprroval process 

Approval Steps 




Now the question is how first_approval__c field will be populated ? 

We need to populate those field as instantly I mean just before going to approval process.

Approach - We created a custom button (Content source is java script) and from button code called a method which will update all the approvers and returns true for successful update, Then we are calling approval process.

Class - 
global class YourClass { 

webservice static Boolean MethodForSettingApprover(Id recordId){
       Boolean isUpdated = false;
  try{
//Update the approvers 
isUpdated = true;
  }
  Catch(Exception ex){
System.debug('Printing exception===='+ex.getmessage());
  }
  return isUpdated;
}
}

Button Code - 
{!REQUIRESCRIPT("/soap/ajax/24.0/connection.js")} 
{!REQUIRESCRIPT("/soap/ajax/10.0/apex.js")} 
function SubmitForApproval() 
var confirmed=confirm("Do you want to continue submitting for approval?"); 

if (confirmed==true) 
var resAppclass = sforce.apex.execute("YourClass","MethodForSettingApprover",{recordId"{!YourObject.Id}"});
var approval;
var processRes;
if(resAppclass == 'true'){
approval = new sforce.ProcessSubmitRequest();
approval.objectId = "{!YourObject.Id}";
processRes = sforce.connection.process([approval]);
}

alert('ProcessRes' + processRes); 
}


No comments: