0

From the controller i am returning list response, which it is having Account, Contacts, Opportunities. Attaching the response image. I need to split different objects data and assign it to different objects attributes. I am unable to do that split from response.getReturnValue().

Component Code.

 <aura:attribute name="ContactList" type="Contact[]" />
<aura:attribute name="AccList" type="Account[]" />
<aura:attribute name="OppList" type="Opportunity[]" />

Helper Code.

var ConList = component.get("c.getSelectedContacts1");
    ConList.setParams
     ({
         accId: AccRecId
     });
    ConList.setCallback(this, function(response) {

         var state = response.getState();
        if(state === "SUCCESS"){
            var allValues = response.getReturnValue();
            console.log("allValues--->>> " + JSON.stringify(allValues));
            component.set('v.AccList', allValues);

        }
        else if(state === "ERROR") {
            var errors = response.getError();
            if(errors){
                if(errors[0] && errors[0].message){
                    console.log("Error Message: " + errors[0].message);
                }
            }
            else{
                console.log("Unknown Error");
            }
        }
    });

And Apex Controller method:

 @AuraEnabled  
public static list<Account> getSelectedContacts1(string accId) {
    return [SELECT id, name,Customer_ID__c, (Select id, Name from Contacts where accountid != null), (Select id, Name, Closedate, stageName  from Opportunities where Accountid != null ) from Account where Customer_ID__c =:accId limit 10];
}

Can someone suggest me to split that data. With those attributes, I am finally displaying in 3 different data tables. enter image description here Thank you.

0

We can use Array.prototype.map to get to an Array<Array<Object>> from Array<Object<Array<Object>>>, then Array.prototype.flat to get an Array<Object>. Here's what that looks like:

var allValues = response.getReturnValue();
var allContacts = allValues.map(acc=>acc.Contacts).flat();
var allOpps = allValues.map(acc=>acc.Opportunities).flat();
| improve this answer | |
  • Awesome @sfdcfox. Working like charm..! – Aryan Feb 21 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.