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
maf1794maf1794 

Add Submit for Approval Button to Lightning Page

Hi,

I'd like to add a Submit for Approval button to my custom Lightning page. I have the button added to my page layout, but it only shows in Salesforce classic. I'm fine with adding this standard button or creating a custom button with the functionality I need. When the button is pressed, I want the record to be locked and a user to be alerted. Upon approval, the record should remain locked; upon rejection, the record should become editable once again.

I've seen multiple posts regarding Submit for Approval with VF pages but not using Lightning. Any suggestions on the best way to implement this?
Daisy ScottDaisy Scott
Yes, we can approve a Record using Lightening Page here is the code:-

Create a simple button in lightening component the name “submit for approval” and send Record id by using component javascriptcontroller  to call apex class method submit and Process Approval Request it will submit  record.

Class:-
public with sharing class LighteningComponentCtr{
@AuraEnabled
public static void submitAndProcessApprovalRequest(account acc)
    {
      User userRecord = [SELECT Id FROM User WHERE name=’userName'];
       // Create an approval request for the account
Approval.ProcessSubmitRequest req1 = new Approval.ProcessSubmitRequest();
req1.setComments('Submitting request for approval');
req1.setObjectId(acc.id);
req1.setSubmitterId(userRecord.id);
req1.setProcessDefinitionNameOrId(‘GiveaprovalProcessName’);// provide approval pr ocess name
req1.setSkipEntryCriteria(true);
        // Submit the approval request for the account
Approval.ProcessResult result1 = Approval.process(req1);
    // First, get the ID of the newly created item
        List<Id>newWorkItemIds = result.getNewWorkitemIds();
        // Instantiate the new ProcessWorkitemRequest object and populate it
Approval.ProcessWorkitemRequest req2 = new Approval.ProcessWorkitemRequest();
req2.setComments('Approving request.');
req2.setAction('Approve');
req2.setNextApproverIds(new Id[] {UserInfo.getUserId()});
 
        // Use the ID from the newly created item to specify the item to be worked
req2.setWorkitemId(newWorkItemIds.get(0));
 
        // Submit the request for approval
Approval.ProcessResult result2 =  Approval.process(req2);
 
        // Verify the results
System.assert(result2.isSuccess(), 'Result Status:'+result2.isSuccess());
System.assertEquals('Approved', result2.getInstanceStatus(), 'Instance Status'+result2.getInstanceStatus());
    }
}
 
Thanks
Salesforce Lightning Application Development (http://www.janbask.com/salesforce-lightning)