Hey guys, today in this post we are going to learn about How to Use SObjectAccessDecision Class to Call one apex method from another method with two different controller and Passing multiple parameters value from one function to another and displaying cross object related record in lightning component Salesforce.
What is SObjectAccessDecision Methods in Salesforce
Contains the results of a call to the Security.stripInaccessible method and methods to retrieve those results.
Find the following are methods for SObjectAccessDecision.
- getModifiedIndexes()
Returns the indexes of sObjects that are modified by the stripInaccessible method. - getRecords()
Returns a list of new sObjects that are identical to the source records, except that they are stripped of fields that fail the field-level security check for the current user. - getRemovedFields()
Returns a map of sObject types to their corresponding inaccessible fields. The map key is a string representation of the sObject type. The map value is a set of strings, which denote the fields names that are inaccessible.
➡ To know more details about SObjectAccessDecision Methods, Click Here
Real time scenarios:- Call multiple apex method Uses of SObjectAccessDecision Class (decision) and display data from inner (Nested) soql query (Opportunity and Report_Cards__c) sObject if Opportunity StageName will be match as “Needs Analysis” in Salesforce.
Files we used to Call Multiple Apex Methods in aura component controller in Salesforce
crossMethodCmp.cmp | Lightning Component | It is used to create a table for Iterate the related list (nested query) of custom object on lightning component |
crossMethodCmpController.js | JavaScript Controller File | It is hold Javascript Init function to get the data from inner (Nested) soql query in lightning component. |
crossMethodCmpHelper.js | JavaScript Helper File | It is hold Javascript Helper Function to get multiple apex method from lightning controller. |
crossMethodCtrl.apxc | Apex Class Controller | It is used to fetch the opportunity record based on record Id. |
opportunityItemCtrl.apxc | Apex Class Controller | It is used to fetch the opportunity and Report_Cards__c sObject record based on opportunity record Id. |
constantsCtrl.apxc | Apex Class Controller | It’s store the diffrent type of string type value for re-reuse to multiple apex method. |
Object:- Opportunity Fields Name:- Name, StageName, CloseDate, Amount |
Custom related Object and their fields:-
Object Name:- Report_Cards__c Fields Name:- Id, Name, Opportunity__c |
Final Output
You can download file directly from github by Click Here.
Other related post that would you like to learn in Salesforce
- Find the below steps
Create Lightning Component
Step 1:- Create Lightning Component : crossMethodCmp.cmp
From Developer Console >> File >> New >> Lightning Component
crossMethodCmp.cmp [Lightning Component File]
|
<aura:component controller="crossMethodCtrl" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
<aura:attribute name="oppListItem" type="Opportunity[]"/>
<aura:attribute name="oppObj" type="sObject"/>
<div class="slds">
<table class="slds-table slds-table_bordered slds-table_col-bordered slds-table--header-fixed" style="border:1px #ccc solid;">
<tr>
<th>Name</th>
<th>StageName</th>
<th>CloseDate</th>
<th>Amount</th>
<th>ReportName</th>
<th>ReportOppty</th>
</tr>
<aura:iteration items="{!v.oppListItem}" var="item">
<tr>
<td>{!item.Name}</td>
<td>{!item.StageName}</td>
<td>{!item.CloseDate}</td>
<td>{!item.Amount}</td>
<aura:iteration items="{!item.Report_Cards__r}" var="reportItem">
<td>{!reportItem.Name}</td>
<td>{!reportItem.Opportunity__c}</td>
</aura:iteration>
</tr>
</aura:iteration>
</table>
<br/><br/>
<!--Start RelatedTopics Section-->
<div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;">
<p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/05/thumbsUpLike.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:16px; font-style:italic; display:inline-block; margin-right:5px;">Don't forget to check out:-</span><a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer" style="text-decoration:none;" data-aura-rendered-by="440:0">An easy way to learn step-by-step online free Salesforce tutorial, To know more Click <span style="color:#ff8000; font-size:18px;" data-aura-rendered-by="442:0">Here..</span></a></strong></p>
<br/><br/>
<p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/07/tickMarkIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:17px; font-style:italic; display:inline-block; margin-right:5px; color:rgb(255 128 0);">You May Also Like →</span> </strong></p>
<div style="display:block; overflow:hidden;">
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<li><a href="https://www.w3web.net/lwc-get-set-lightning-checkbox-value/" target="_blank" rel="noopener noreferrer">How to get selected checkbox value in lwc</a></li>
<li><a href="https://www.w3web.net/display-account-related-contacts-in-lwc/" target="_blank" rel="noopener noreferrer">how to display account related contacts based on AccountId in lwc</a></li>
<li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to create lightning datatable row actions in lwc</a></li>
<li><a href="https://www.w3web.net/if-and-else-condition-in-lwc/" target="_blank" rel="noopener noreferrer">how to use if and else condition in lwc</a></li>
<li><a href="https://www.w3web.net/get-selected-radio-button-value-and-checked-default-in-lwc/" target="_blank" rel="noopener noreferrer">how to display selected radio button value in lwc</a></li>
</ul>
</div>
<div style="width: 50%; float:left; display:inline-block">
<ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;">
<li><a href="https://www.w3web.net/display-account-related-contacts-lwc/" target="_blank" rel="noopener noreferrer">display account related contacts based on account name in lwc</a></li>
<li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to insert a record of account Using apex class in LWC</a></li>
<li><a href="https://www.w3web.net/fetch-picklist-values-dynamic-in-lwc/" target="_blank" rel="noopener noreferrer">how to get picklist values dynamically in lwc</a></li>
<li><a href="https://www.w3web.net/edit-save-and-remove-rows-dynamically-in-lightning-component/" target="_blank" rel="noopener noreferrer">how to edit/save row dynamically in lightning component</a></li>
<li><a href="https://www.w3web.net/update-parent-object-from-child/" target="_blank" rel="noopener noreferrer">update parent field from child using apex trigger</a></li>
</ul>
</div>
<div style="clear:both;"></div>
<br/>
<div class="youtubeIcon">
<a href="https://www.youtube.com/channel/UCW62gTen2zniILj9xE6LmOg" target="_blank" rel="noopener noreferrer"><img src="https://www.w3web.net/wp-content/uploads/2021/11/youtubeIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/> <strong>TechW3web:-</strong> To know more, Use this <span style="color: #ff8000; font-weight: bold;">Link</span> </a>
</div>
</div>
</div>
<!--End RelatedTopics Section-->
</div>
</aura:component>
Create JavaScript Controller
Step 2:- Create Lightning Component : crossMethodCmpController.js
From Developer Console >> File >> New >> Lightning Component >> JavaScript Controller
crossMethodCmpController.js [JavaScript Controller]
|
({
doInit:function(component,event,helper){
helper.doInitHelper(component);
},
})
Create JavaScript Helper
Step 3:- Create Lightning Component : crossMethodCmpHelper.js
From Developer Console >> File >> New >> Lightning Component >> JavaScript Helper
crossMethodCmpHelper.js [JavaScript Helper File]
|
({
doInitHelper : function(component,event,helper) {
var action = component.get("c.getOppDetail");
action.setParams({
"recId":component.get("v.recordId"),
});
action.setCallback(this,function(response){
var state = response.getState();
if(state=='SUCCESS'){
var result = response.getReturnValue();
//alert(' resultAAA ' + result.Name);
//alert('result ' + JSON.stringify(result));
component.set("v.oppListItem",result);
component.set("v.oppObj",result);
var reportCard = component.get("v.oppObj.Report_Cards__r[0].Name");
//alert('reportCard ' + reportCard);
}
});
$A.enqueueAction(action);
},
})
Create Apex Class Controller
Step 4:- Create Apex Class : crossMethodCtrl.apxc
From Developer Console >> File >> New >> Apex Class
crossMethodCtrl.apxc [Apex Class Controller]
|
public class crossMethodCtrl {
@AuraEnabled
public static Opportunity getOppDetail(String recId){
final String needsAnalys = 'Needs Analysis';
Opportunity opp = opportunityItemCtrl.getOpportunityList(recId);
Opportunity returnOpp;
system.debug('opp ' + opp);
IF(opp.StageName == constantsCtrl.stageNeedsAnalysis){
returnOpp = opportunityItemCtrl.getNeedsAnalys(recId, opp.StageName);
system.debug('returnOppAAA ' + returnOpp);
}
RETURN returnOpp;
}
}
Create Apex Class Controller
Step 5:- Create Apex Class : opportunityItemCtrl.apxc
From Developer Console >> File >> New >> Apex Class
opportunityItemCtrl.apxc [Apex Class Controller]
|
public class opportunityItemCtrl {
public static Opportunity getOpportunityList(String recId){
SObjectAccessDecision decision = Security.stripInaccessible(
AccessType.READABLE,
[SELECT Id,Name, StageName, CloseDate, Amount FROM Opportunity WHERE Id =: recId]);
system.debug('extractOpportunity' + extractOpportunity(decision));
RETURN extractOpportunity(decision);
}
public static Opportunity getNeedsAnalys(String recId, String stage){
SObjectAccessDecision decision = Security.stripInaccessible(
AccessType.READABLE,
[SELECT Id,Name, StageName, CloseDate, Amount, (SELECT Id, Name, Opportunity__c FROM Report_Cards__r ORDER BY createdDate DESC LIMIT 1)
FROM Opportunity WHERE StageName =: stage AND Id =: recId]);
system.debug('extractOpportunity222' + extractOpportunity(decision));
RETURN extractOpportunity(decision);
}
private static Opportunity extractOpportunity(SObjectAccessDecision decision){
IF(!decision.getRecords().isEmpty()){
RETURN (Opportunity)decision.getRecords()[0];
}
ELSE{
RETURN NEW Opportunity();
}
}
}
Create Apex Class Controller
Step 6:- Create Apex Class : constantsCtrl.apxc
From Developer Console >> File >> New >> Apex Class
constantsCtrl.apxc [Apex Class Controller]
|
public WITHOUT sharing class constantsCtrl {
public static final String stageNeedsAnalysis = 'Needs Analysis';
public static final String stageValueProposition = 'Value Proposition';
public static final String stageNegotiationReview = 'Negotiation/Review';
public static final String amazonS3Manage = 'amazon s3 manage';
}
Further post that would you like to learn in Salesforce
Can we call one method from another method in Apex Salesforce?
Yes
What is virtual class in Apex?
Virtual class are functional classes and can be instantiated or extended without then need of overriding methods.
What is the use of anonymous window in Apex?
The Execute Anonymous Apex tool in the Developer Console runs the Apex code you enter using ExecuteAnonymous and generates a debug log with the results of the execution. Warning If you call a class that contains a testMethod , all DML statements of the test method execute.
What is apex when doing nails?
The apex should be the highest part of an enhancement, located at the weakest point and created to give the nail balance and strength. It should be visible from every angle but is most noticeable from the side view. The position of the apex varies in different shapes and lengths.
Related Topics | You May Also Like
Our Free Courses →
👉 Get Free Course →
📌 Salesforce Administrators 📌 Salesforce Lightning Flow Builder 📌 Salesforce Record Trigger Flow Builder |
👉 Get Free Course →
📌 Aura Lightning Framework 📌 Lightning Web Component (LWC) 📌 Rest APIs Integration |