+ Start a Discussion

Add Submit for Approval Button to Lightning Page


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.

public with sharing class LighteningComponentCtr{
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.setProcessDefinitionNameOrId(‘GiveaprovalProcessName’);// provide approval pr ocess name
        // 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.setNextApproverIds(new Id[] {UserInfo.getUserId()});
        // Use the ID from the newly created item to specify the item to be worked
        // 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());
Salesforce Lightning Application Development (http://www.janbask.com/salesforce-lightning)