Apex · Lightning · Salesforce

Triggering approval process from Napili template

Agenda of this article is to create an Approval process for Napili Template.


  • CustomObject__c object (custom object) is used and have lookup relation to Contact.
  • Component name is “Submit_for _approval”
  • Approval Process is created on the CustomObject__c.
  • CustomObject__c have multiple approval processes set up with different entry criteria.

To achieve this, I am creating lighting component named “Submit_for_approval”

<aura:component controller="Submitforapprovalcustom" implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global">

    <aura:handler name="init" value="{!this}" action="{!c.statuscheck}" />
    <aura:attribute name="recordId" type="String" />
<div class="slds-align_absolute-center slds-p-top_xx-large">
Are you sure ?
<ui:button label="Yes" press="{!c.submit}" />


Js controller:


    statuscheck: function(component) {
        var action1 = component.get("c.getstat");
        var id = component.get("v.recordId")
            "recId": id
        action1.setCallback(this, function(response) {
            var state = response.getState();
            console.log('state' + state);
            if (state === "SUCCESS") {

                component.set("v.status", response.getReturnValue());

    submit: function(component) {

        var stat = component.get("v.status");

        var toastevent = $A.get("e.force:showToast");
        if (stat === "Approved" || stat === "Submitted") {

                "title": "Error!",
                "type": "error",
                "message": "Can not submit again, Alredy Approved or Submitted!"

            var dismissActionPanel = $A.get("e.force:closeQuickAction");
        } else {
            var action = component.get("c.submitForApprovalone");
            var id = component.get("v.recordId")
                "recId": id

            action.setCallback(this, function(response) {
                var state = response.getState();
                console.log('state' + state);
                var toastEvent = $A.get("e.force:showToast");
                if (state === "SUCCESS") {
                        "title": "Success!",
                        "type": "success",
                        "message": "Successfully submitted for approval."
                    var dismissActionPanel = $A.get("e.force:closeQuickAction");






Here we are considering thatCustomObject__c has two approval process with different criteria.

To call different approval process we are providing if condition, that will be the entry criteria in the approval process.

if (Stage[0].Name != 'Stage 4') {
    //code block<span 				data-mce-type="bookmark" 				id="mce_SELREST_start" 				data-mce-style="overflow:hidden;line-height:0" 				style="overflow:hidden;line-height:0" 			></span>

ProcessSubmitRequest Class
Use the ProcessSubmitRequest class to submit a record for approval. You must specify the Approval namespace when creating an instance of this class.

Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();

Sets the ID of the record to be submitted for approval.


NextApproverIds – if needed

req.setNextApproverIds(new Id[] { UserInfo.getUserId()

Use the ProcessSubmitRequest class to submit a record for approval. A ProcessResult object is returned by the process method. You must specify the Approval namespace when creating an instance of this class

Approval.ProcessResult result = Approval.process(req);

A Boolean value that is set to true if the approval process completed successfully; otherwise, it is set to false.

Complete code:

Apex Class Submitforapprovalcustom

public without sharing class Submitforapprovalcustom {

    public static void submitForApprovalone(Id recId) {

        List < CustomObject__c > stage = new List();
        Stage = [select id, Name from CustomObject__c where Id =: recId];
        if (Stage[0].Name != 'Stage 4') {
            Approval.ProcessSubmitRequest req = new
            req.setNextApproverIds(new Id[] {
            Approval.ProcessResult result = Approval.process(req);
            System.debug('Submitted for approval successfully:

            if (Stage[0].Name == 'Stage 4') {
                //code block


        Public static string getstat(Id recId) {
            List stg = new List();
            stg = [select Name, Approval_Status__c from CustomObject__c
                where = recId
            String str = stg[0].Approval_Status__c;
            return str;



To get submit for approval button in communities I am going to create a New Action.

  • Action Type – Lighting component
  • Lighting Component- Name of your component
  • Label- submit for Approval

Screen Shot 2017-12-29 at 4.40.50 PM.png

This slideshow requires JavaScript.

Important Links:


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s