1

I want to display the contact names and opportunity names where AccountID is not null, I am able to display the contact names, can someone please help me to display the opportunity names too?

Below is the code I am using to display the data.

component:

<aura:component controller="OpConAccountApexController">
    <aura:attribute name="ListOfData" type="Contact[]"/>
    <aura:attribute name="ColumnNames" type="List" />

    <aura:handler name="init" value="{!this}" action="{!c.Init}" />

    <!--for columns there would be an attribute type List-->
    <!--for data there would be an attribute of Sobject array -->
    <lightning:datatable data="{!v.ListOfData}"
                         columns="{!v.ColumnNames}" 
                         keyField="Id" />
</aura:component>

Controller:

({
    Init : function(component, event, helper) {
        //setting the fields of the columns 
        component.set('v.ColumnNames', [
            {label: 'Contact Name', fieldName: 'Name', type: 'Text'},
            {label: 'Opportunity Name', fieldName: 'opportunity.Name', type: 'Text'}
        ]);
        helper.getData(component);
    }
})

Helper:

({
    getData : function(component) {
        var action = component.get('c.getDetails');

        action.setCallback(this, function(response) {
           var state =  response.getState();
            if(state === "SUCCESS") {
                //ContactList attribute of Contact array type
                console.log('values: '+ response.getReturnValue());
                component.set('v.ListOfData', response.getReturnValue());
            }else if(state ==="ERROR") {
                var error = response.getError();
                console.log('Failing with the: '+ error);
            }
        });
        $A.enqueueAction(action);
    }
})

Apex Controller:

public class OpConAccountApexController {

    @auraEnabled
    public static List<Contact> getDetails() {

        List<Contact> Contacts = [SELECT ID, Name,(SELECT Name FROM Opportunities where AccountID != null)
                                  FROM Contact where AccountID != null];

        return Contacts;        
    }
}
  • As the inner Query will return list of Opportunity, I guess you can't use datatable here, you need to create your own table – salesforce Developer Aug 1 '18 at 17:49
  • Thank you so much for your response I will try with the tables but could you please tell me how to retrieve the fields from the inner query? – gs650x Aug 1 '18 at 18:18
1

If you don't want to create your own table and you simply want a list of opportunities, you can preprocess the data when it arrives from the server.

You would perform this from here (probably best as a seperate method, but for now just do it the way I do)

if(state === "SUCCESS") {
    var contacts = response.getReturnValue();
    var oppNames = [];
    contacts.forEach(contact => {
      if (contact.Opportunities && contact.Opportunities.length > 0){
        contact.Opportunities.forEach(opp => {
          oppNames.push(opp.Name);
        });
        contact['Opportunities'] = oppNames.join(' - ');
      }
    });
    component.set('v.ListOfData', contacts);
}

This just grabs the Opportunities array and turns it into a string on the contact object that you can use in the table.

One thing... check the name of the Opportunities array - I'm not totally sure if it's Opportunity or Opportunities. Also, remember, Lightning is case sensitive, so make sure you get those names exactly correct!

  • Thank you for your response but it has multiple syntax errors. – gs650x Aug 1 '18 at 18:16
  • oops just realised I'd declared the oppNames as a String. changed to var. What else is wrong? – Caspar Harmer Aug 1 '18 at 18:32
  • the length and the lambda expression and opportunities is not an array name, I request you to please check my apex controller as well. Thank you so mcuh – gs650x Aug 1 '18 at 18:47
  • I don't actually have this relationship in my org... but using account.Opportunities worked fine. I added a null check for when there are no Opportunities for a given record. – Caspar Harmer Aug 1 '18 at 19:56

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.