0

I have a requirement to select child records of selected record on a detail page. This Lightning component, will be used on Account record page and that Account record's related contact records will be displayed with checkbox to select on the page.

I found this code handy to some extent but it displays all the contact records from the org that can be added as child to the selected Account record. In a nutshell, this works in the reverse way of what I am expecting where i need to display already available child contact records with a checkbox to select.

Can anyone please suggest the possible approach w.r.t my requirement above.

Thanks!

1 Answer 1

0

The code in the link has all of the ingredients needed for the solution that you're looking for :)

You could add a parameter to the example getContacts method that receives the Account Id and filter the query by that Id:

@AuraEnabled
public static List<Contact> getContacts(Id accountId) {
    List<Contact> totalList = new List<Contact>();
    for(Contact cc : [SELECT Id, Name, Email FROM Contact WHERE AccountId=:accountId]) {
        totalList.add(cc);
    }   
    return totalList;
}

Finally, just make sure that the Lightning Component's JS Controller/Helper passes the Record Id to that parameter in the Action:

var action = component.get('c.getContacts');

action.setParams({
    "accountId": component.get("v.recordId")
});

action.setCallback(this, function(actionResult) {
    var state = actionResult.getState();
    if (state === 'SUCCESS') {
        //set response value in ChildRecordList attribute on component.
        component.set('v.ChildRecordList', actionResult.getReturnValue());
    }
});
$A.enqueueAction(action);
2
  • while trying your suggested code, I am getting the following error: "Failed to save RelatedListController.js: c.RelatedList: Failed to parse CONTROLLER for js://c.ChildList: org.auraframework.util.json.JsonStreamReader$JsonStreamParseException: Expected ':' [27, 9]: 'action': Source" Please suggest
    – SK_112
    Aug 19, 2020 at 6:27
  • Could you possibly add a snippet of your RelatedListController.js controller code? Particularly line 27, where the error is happening?
    – Instread
    Aug 19, 2020 at 15:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .