ABSYZ ABSYZ

  • Home

    Home

  • About us

    Who We Are

  • Our Expertise

    What we Do

  • Our Approach

    How We Do It

  • Products

    What We Made

  • Industries

    Who We Do It For

  • Clients

    Whom We Did It For.

  • Article & Blogs

    What Experts Think

  • Careers

    Join The Team

  • Get In Touch

    Let’s Get Started

ABSYZ

Triggering approval process from Napili template

Home / Article & Blogs / Apex / Triggering approval process from Napili template
By kanchanbaghel inApex, Lightning, Salesforce

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”

[sourcecode language=”java”]
<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>
[/sourcecode]

Js controller:

[sourcecode language=”java”]
({

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

}

})
[/sourcecode]

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.

[sourcecode language=”java”]
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>
}
[/sourcecode]

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.

[sourcecode language=”java”]
Approval.ProcessSubmitRequest req = new Approval.ProcessSubmitRequest();
[/sourcecode]

setObjectId(recordId)

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

[sourcecode language=”java”]
req.setObjectId(recId);
[/sourcecode]

NextApproverIds

– if needed

[sourcecode language=”java”]
req.setNextApproverIds(new Id[] { UserInfo.getUserId()
[/sourcecode]

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

[sourcecode language=”java”]
Approval.ProcessResult result = Approval.process(req);
[/sourcecode]

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

[sourcecode language=”java”]
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;
}

}
[/sourcecode]

 

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

Important Links:

ProcessRequest
ProcessResult
ProcessSubmitRequest

approval processcommunity usersnapili template
104
Like this post
4 Posts
kanchanbaghel

Search Posts

Archives

Categories

Recent posts

BioAsia 2023 in Hyderabad: An Annual International Event

BioAsia 2023 in Hyderabad: An Annual International Event

The Role Of Marketing in Small & Medium Enterprises

The Role Of Marketing in Small & Medium Enterprises

Salesforce For Retail: How Salesforce CRM Can Help Retailers

Salesforce For Retail: How Salesforce CRM Can Help Retailers

What is ChatGPT & How Does It Work?

What is ChatGPT & How Does It Work?

What Is Graphic Design? (Executive Summary 2023)

What Is Graphic Design? (Executive Summary 2023)

  • Previous PostApex Documentation Using ApexDoc
  • Next PostCSV PARSING AND GENERATING CHARTS IN LIGHTNING COMPONENTS

Related Posts

Salesforce For Retail: How Salesforce CRM Can Help Retailers
Salesforce

Salesforce For Retail: How Salesforce CRM Can Help Retailers

Introduction To Copado Devops Tool
Salesforce

Introduction To Copado Devops Tool

What is Salesforce Code Builder?
Salesforce

What is Salesforce Code Builder?

Automation in Healthcare And Its Benefits
Health Cloud Salesforce

Automation in Healthcare And Its Benefits

Leave a Reply (Cancel reply)

Your email address will not be published. Required fields are marked *

*
*

ABSYZ Logo

INDIA | USA | UAE

  • About us
  • Article & Blogs
  • Careers
  • Get In Touch
  • Our Expertise
  • Our Approach
  • Products
  • Industries
  • Clients
  • White Papers

Copyright ©2022 Absyz Inc. All Rights Reserved.

youngsoft
Copy
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “ACCEPT ALL”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent. Privacy Policy
Cookie SettingsREJECT ALLACCEPT ALL
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled

Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.

CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.

Functional

Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.

Performance

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

Analytics

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.

Advertisement

Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.

Others

Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.

SAVE & ACCEPT