+ Start a Discussion

get all opportunity of selected AccountAccounts

hii there iam new to lightning componants
i want to get all the records of account on my componat and when i click on perticuler single account record it should be show related all Opportunities
in additional when i select a single opportunity record i will popup Stage and amount
so can you plz help me with that
Ajay K DubediAjay K Dubedi
Hi Rahul,

I have gone through your question and came up with a solution. It should work for you.
Here is my code - 
//Component - 

<aura:component controller="AccountOppController" implements="force:appHostable,flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId,forceCommunity:availableForAllPageTypes,force:lightningQuickAction" access="global" >
    <aura:attribute name="accList" type="Account[]" />
    <aura:attribute name="oppList" type="Opportunity[]" />       
    <aura:attribute name="closeDate" type="string" />    
    <aura:attribute name="stageName" type="string" />  
    <aura:attribute name="isOpen" type="boolean" default="false"/>
    <aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <lightning:select name="list1" label="Accounts" onchange="{! c.getOpps }" >
        <option value="- Select An Account -" text="- Select An Account -" />
        <aura:iteration items="{!v.accList}" var="acc">
            <option text="{!acc.Name}" value="{!acc.Id}"/>
    <lightning:select name="list2" label="Opportunities" onchange="{! c.getInfo }">    
        <option value="- Select An Opportunity -" text="- Select An Opportunity -" />
        <aura:iteration items="{!v.oppList}" var="opp">
            <option text="{!opp.Name}" value="{!opp.Id}"/>
    <aura:if isTrue="{!v.isOpen}">                        
        <section role="dialog" tabindex="-1" aria-labelledby="modal-heading-01" aria-modal="true" aria-describedby="modal-content-id-1" class="slds-modal slds-fade-in-open">
            <div class="slds-modal__container">                   
                <header class="slds-modal__header">
                    <lightning:buttonIcon iconName="utility:close"
                                          onclick="{! c.closeModel }"
                    <h2 id="modal-heading-01" class="slds-text-heading_medium slds-hyphenate">Opportunity Info</h2>
                <div class="slds-modal__content slds-p-around_medium" id="modal-content-id-1">
                <footer class="slds-modal__footer">
                    <lightning:button variant="neutral" 
                                      onclick="{! c.closeModel }"/>                    
        <div class="slds-backdrop slds-backdrop_open"></div>                      

//Controller - 

    doInit : function(component, event, helper) {        
        helper.doInitHelper(component, event, helper);
    getOpps : function(component, event, helper) {       
        helper.getOppsHelper(component, event, helper);
    getInfo : function(component, event, helper) {         
        helper.getInfoHelper(component, event, helper);
    closeModel: function(component, event, helper) {     
      component.set("v.isOpen", false);

//Helper - 

    doInitHelper : function(component, event, helper) {                 
        var action =component.get("c.getAccounts");
        console.log('The action value is: '+action);
        action.setCallback(this, function(a){              
            component.set("v.accList", a.getReturnValue());            
            console.log('The account list is :'+JSON.stringify(a.getReturnValue()));          
    getOppsHelper : function(component, event, helper) {                 
        var accId = event.getSource().get("v.value");        
        var action2 = component.get("c.getOppList");
        console.log('The action value is: '+action2);        
            accId : accId
        action2.setCallback(this, function(a){              
            component.set("v.oppList", a.getReturnValue());            
            console.log('The opportunity list is :'+JSON.stringify(a.getReturnValue()));          
    getInfoHelper : function(component, event, helper) {                  
        var oppId = event.getSource().get("v.value");
        if(oppId != '- Select An Opportunity -') {
            var action3 = component.get("c.getDetails");
            console.log('The action value is: '+action3);        
                oppId : oppId
            action3.setCallback(this, function(a){                         
                component.set("v.stageName", 'Stage Name - '+a.getReturnValue()[0]);  
                component.set("v.closeDate", 'Close Date - '+a.getReturnValue()[1]);                               
                console.log('The field list is :'+JSON.stringify(a.getReturnValue()));          

//Server Side Controller(Apex Class) - 

public class AccountOppController {
    public static List<Account> getAccounts() {
        return [Select Id, Name From Account];
    public static List<Opportunity> getOppList(String accId) {
        return [Select Id, Name From Opportunity Where AccountId = :accId];
    public static List<String> getDetails(String oppId) {        
        Opportunity opp = [Select Id, Name, StageName, CloseDate From Opportunity Where Id = :oppId Limit 1];
        List<String> fldInfo = new List<String>();                                  
        return fldInfo;

//Lightning App - 

<aura:application extends="force:slds">   

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Ajay Dubedi