Agenda of this article is to create an Approval process for Napili Template.
Assumptions:
- 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}" /> </aura:component>
Js controller:
({ statuscheck: function(component) { var action1 = component.get("c.getstat"); var id = component.get("v.recordId") action1.setParams({ "recId": id }); action1.setCallback(this, function(response) { var state = response.getState(); console.log('state' + state); if (state === "SUCCESS") { component.set("v.status", response.getReturnValue()); } }); $A.enqueueAction(action1); }, submit: function(component) { var stat = component.get("v.status"); var toastevent = $A.get("e.force:showToast"); if (stat === "Approved" || stat === "Submitted") { toastevent.setParams({ "title": "Error!", "type": "error", "message": "Can not submit again, Alredy Approved or Submitted!" }); toastevent.fire(); var dismissActionPanel = $A.get("e.force:closeQuickAction"); dismissActionPanel.fire(); } else { var action = component.get("c.submitForApprovalone"); var id = component.get("v.recordId") action.setParams({ "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") { toastEvent.setParams({ "title": "Success!", "type": "success", "message": "Successfully submitted for approval." }); toastEvent.fire(); var dismissActionPanel = $A.get("e.force:closeQuickAction"); dismissActionPanel.fire(); } }); $A.enqueueAction(action); } } })
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();
setObjectId(recordId)
Sets the ID of the record to be submitted for approval.
req.setObjectId(recId);
NextApproverIds – if needed
req.setNextApproverIds(new Id[] { UserInfo.getUserId()
ProcessResult
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);
isSuccess()
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 { @AuraEnabled 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 Approval.ProcessSubmitRequest(); req.setObjectId(recId); req.setNextApproverIds(new Id[] { UserInfo.getUserId() }); Approval.ProcessResult result = Approval.process(req); System.debug('Submitted for approval successfully: '+result.isSuccess()); } if (Stage[0].Name == 'Stage 4') { //code block } } @AuraEnabled 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
Important Links: