This post is a part of the daily blog series
A Tip A Day, daily dosage of learning!
Day #2 – Display custom popup message while Submitting for Approval
Standard Approval Process Feature: When you setup Standard Approval process on any object, a standard button ‘Submit For Approval’ will be added to the page layout of the object’s record. When you click on the button, it will show a “standard” popup message asking if you want to submit for approval.
Requirement: The requirement is to change that standard message and display a different message “Is the contract signed” and if they click Yes, then the approval process should kick in. Salesforce doesn’t allow to change the popup message. Here’s the solution.
Solution:
- Create a custom button and add this code. In this code as you can see we are adding a different message – “Submit for Approval only if the contract is signed”. Then we call a class called “ApprovalProcessor” with a method “submitAndProcessApprovalRequest”
{!REQUIRESCRIPT("/soap/ajax/29.0/connection.js")} {!REQUIRESCRIPT("/soap/ajax/29.0/apex.js")} if(confirm('Submit for Approval only if the Contract is signed, else click Cancel')){ sforce.apex.execute("ApprovalProcessor","submitAndProcessApprovalRequest"); }
- 2. Create an apex class: Create the Class Approval Processor with a static method. The method calls the Approval process you already setup – PTO_Request_Process. Below is the code
public class ApprovalProcessor { | |
webservice void submitAndProcessApprovalRequest() { | |
// Create an approval request for the account | |
Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest(); | |
req1.setComments('Submitting request for approval.'); | |
req1.setObjectId(a.id); | |
// Submit on behalf of a specific submitter | |
req1.setSubmitterId(user1.Id); | |
// Submit the record to specific process and skip the criteria evaluation | |
req1.setProcessDefinitionNameOrId('PTO_Request_Process'); | |
req1.setSkipEntryCriteria(true); | |
// Submit the approval request for the account | |
Approval.ProcessResult result = Approval.process(req1); | |
// Verify the result | |
System.assert(result.isSuccess()); | |
System.assertEquals( | |
'Pending', result.getInstanceStatus(), | |
'Instance Status'+result.getInstanceStatus()); | |
) |
So, in this process you are still using the standard out of the box functionality of Approval Process but just that the initiation is done from a custom button instead of standard button.
Hope this helps!
Read all other tips of the blog series here – A Tip A Day, daily dosage of learning!
One Reply to “A Tip A Day #2 – Display custom popup message while Submitting for Approval”