You need to sign in to do that
Don't have an account?
SV M
Display Contacts and Opportunities related to Account in Lightning
Hi, I wanted to show Accounts with checkbox on my page. If I select an account and click Next button I want to display contacts related to that Account. Can someone help me how to do this using Aura Components since I am new to Aura Components. Thanks in Advance...
Below is the code you will get a list of account record while you will select account then its related opportunity and contacts will show.
<!-- AURA Comonent -->
<aura:component controller="AccountApex" 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="accountList" type="list"/>
<aura:attribute name="selectedAccount" type="account"/>
<aura:attribute name="contactList" type="list"/>
<aura:attribute name="opportunityList" type="list"/>
<lightning:select name="selectItem" label="Select Account" aura:id="ac" onchange="{!c.getDetail}" >
<aura:iteration items="{!v.accountList}" var="acc">
<option label="{!acc.Name}" value="{!acc.Id}" />
</aura:iteration>
</lightning:select>
<lightning:select label="contact list" >
<aura:iteration items="{!v.contactList}" var="con">
<option label="{!con.LastName}" value="{!con.Id}"/>
</aura:iteration>
</lightning:select>
<lightning:select label="Opportunity list" >
<aura:iteration items="{!v.opportunityList}" var = "op">
<option label="{!op.Name}" value="{!op.Id}"/>
</aura:iteration>
</lightning:select>
</aura:component>
// CONTROLLER
({
doInit : function(component, event, helper) {
helper.getAllAccount(component, event, helper);
},
getDetail: function(component, event, helper){
helper.helperGetDetail(component, event, helper);
helper.helperGetOpportunity(component, event, helper);
}
})
// HELPER
({
getAllAccount : function(component, event, helper) {
var action=component.get('c.fetchAccount');
action.setCallback(this,function(response){
console.log('response===> '+JSON.stringify(response.getReturnValue()));
if(response.getReturnValue()!=null){
console.log('success');
component.set('v.accountList',response.getReturnValue());
}
});
$A.enqueueAction(action);
},
helperGetDetail: function(component, event, helper){
var acid=component.find('ac').get('v.value');
console.log(acid);
var action=component.get('c.getContact');
action.setParams({
accid:acid
});
action.setCallback(this,function(response){
console.log('response===> '+JSON.stringify(response.getReturnValue()));
if(response.getReturnValue()!=null){
console.log('success');
component.set('v.contactList',response.getReturnValue());
}
});
$A.enqueueAction(action);
},
helperGetOpportunity : function(component, event, Helper){
var opid = component.find('ac').get('v.value');
console.log(opid);
var action = component.get('c.getopportunity');
action.setParams({
accid:opid
});
action.setCallback(this, function(response){
console.log('opp response==> '+JSON.stringify(response.getReturnValue()));
if(response.getReturnValue() != null){
console.log("OPP success");
component.set('v.opportunityList', response.getReturnValue());
console.log("Pass");
}
});
$A.enqueueAction(action);
}
})
// APEX
public class AccountApex {
@auraEnabled
public static list<account> fetchAccount(){
list<account> accList= new list<account>();
accList=[select name from account];
return accList;
}
@auraEnabled
public static list<contact> getContact(id accid){
list<contact> conList= new list<contact>();
conList=[select lastname from contact where accountid =:accid];
return conList;
}
@auraEnabled
public static list<opportunity> getopportunity(id accid){
list<opportunity> opList = new list<opportunity>();
opList = [select name from opportunity where accountid =: accid];
return opList;
}
}
Please mark it as Best Answer if it helps you.
Thanks & Regards
Suraj Tripathi
Try Below Code Please Mark It As Best Answer If It Helps
Thank You!