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
Alex Valavanis 10Alex Valavanis 10 

Display custom popup message while "Submitting for Approval"

Hello, I want to create a popup message alerting the users when they click on the Standard Button "Submit For Approval". I found the below link on how to do this, however i am receiving an error (Screenshot). Could you please help me? I have no experience in coding.

The Approval Process is called: "Distribution_Agreement_Onboarding"

(https://sfdcfanboy.com/2017/07/24/a-tip-a-day-2-display-custom-popup-message-while-submitting-for-approval/)

User-added image
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('Distribution_Agreement_Onboarding');

        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());
         )

 
SwethaSwetha (Salesforce Developers) 
HI Alex,
Replace the ) in line 28(last line of this code) with  }} to fix the issue.

I also see that there are few other issues with code when I tried this code snippet in my org - like it threw Compile Error: Variable does not exist: a at line 9 column 26 

You need to create an account and user object variable to fix the errors.

Try below code snippet
Global class ApprovalProcessor {

 webservice static  void submitAndProcessApprovalRequest() {
 Account a = new Account(Name='Test',annualRevenue=100.0);
        insert a;
        User user1 = [SELECT Id FROM User WHERE Alias='SomeStandardUser'];
        // 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('Distribution_Agreement_Onboarding');

        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());
         }}

If this information helps, please mark the answer as best.Thank you
 
Alex Valavanis 10Alex Valavanis 10
Hello @Swetha. Thank you but i'm afraid I have another issue now:

User-added image
SwethaSwetha (Salesforce Developers) 
HI Alex,
I did foresee upcoming errors and I was in the process of editing my comment. Please try the code snippet I added in my first comment.

If this information helps, please mark the answer as best.Thank you
SwethaSwetha (Salesforce Developers) 
I also recommend reviewing https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_process_example.htm
Alex Valavanis 10Alex Valavanis 10
@Swetha Thank so much. I could save

I also created a custom button on the Distribution Agreement Object (This is where the Approval Process is) Is what i have done correct?
How can i add the button to the page layout? I cannot see in the page layout nor in the Seach layout (?)

User-added image
SwethaSwetha (Salesforce Developers) 
You should be creating custom button on Distribution Agreement object.
Then Navigate to Setup> Object Manager> Click Distribution Agreement object > You should be able to see page layouts on the left side and you should be able to add the custom button created on this Distribution Agreement object
Alex Valavanis 10Alex Valavanis 10
Hey Swetha Thank you. What does the Appex Class say? I can see it mentions "Account a = new Account(Name='Test',annualRevenue=100.0);"
How is this related to my scenario?

Also - Custom button doesn't show in my page layout! Please see screenshots. I have named the button "Test Button"

User-added image

User-added imageUser-added image
SwethaSwetha (Salesforce Developers) 
HI Alex,
You will need to customize the code to reflect the custom object-"Distribution Agreement" instead of the Account. Implies you need to edit the mentioned line in your comment to 

Distribution_Agreement__c d = new Distribution_Agreement__c (Name='Test');"

Regarding the button on page layout, I checked the screenshots. Please ensure you are on the right page layout of the Distribution Agreement record.

If this information helps, please mark the answer as best.Thank you