0

I have a Lightning component in which I want to display a list of records related to an Account. I currently have this (but it doesn't work):

My Component display:

<aura:if isTrue="{!not(empty(v.issues))}">
    <div class="slds-form-element__row">
        <div class="slds-form-element slds-size_1-of-2">
            <lightning:combobox name="issues" label="Issue" placeholder="Select an Issue" options="{!v.issues}" onchange="{!c.selectIssue}"/>                    
        </div>
    </div>
</aura:if>

The Helper code:

var issueOpt = response.getReturnValue().issues;
var issues = [];
if(issueOpt.length > 0){
    for (var i = 0; i < issueOpt.length; i++){
        //var issueOption = issueOpt[i].toString();
        var item = {
            "label": issueOpt[i].Id.toString(),
            "value": issueOpt[i].Name.toString(),
        };
        issues.push(item);                            
    }
}
component.set("v.issues", issues);

The Controller simply runs a query that populates a list of 'issue' object records (Id, Name). It falls over in the Helper code - what am I doing wrong?

Update: In editing my code as @sfdcfox has suggested in my Helper class, I have also changed the attribute in my component from a list to a map:

<aura:attribute name="issues" type="Map" default="{}"/>

This is in my controller:

@AuraEnabled
public List<Issue__c> issues {get;set;} // List of issues for Account   

....., and the query:

List<AggregateResult> incomes = [Select Issue__c, Issue__r.Name 
                           From Income__c Where Account__c =: account.Id
                           group by Issue__c, Issue__r.Name];
Issue__c issue = new Issue__c();
issues = new list<Issue__c>();
for(AggregateResult aggr:incomes){ 
    issue.Id = (Id)aggr.get('Issue__c');
    issue.Name = (String)aggr.get('Name');
    issues.add(issue);
}

1 Answer 1

3

We'd have to see the return value from your Apex, but the proper code to populate this usually goes something like this:

var retVal = response.getReturnValue().issues;
var opts = retVal.map(opt => ({ value: opt.Id, label: opt.Name }));
component.set("v.issues", opts);

You would not ordinarily call toString() on the values returned from Apex, as they are already string values.

5
  • and calling toString() on null or undefined vars throws Uncaught TypeError so the issue might be there. Aug 13, 2019 at 20:42
  • @OlehBerehovskyi indeed, I just didn't want to make too many assumptions. Although, assuming it's a list of record values, Name would never be null, nor would Id (both are "required" for most types of records in Salesforce).
    – sfdcfox
    Aug 13, 2019 at 20:46
  • @sfdcfox I've put your code in and tried it, but am getting this error: Uncaught Error in $A.getCallback() [Cannot read property 'map' of undefined] - this is when the list is empty. I need to catch that - how would I do that?
    – Irene
    Aug 13, 2019 at 20:46
  • @Irene Empty, or null? Ideally, you should not be returning a null value back from Apex. This makes it more complicated twice: once in Apex to return a null value, then again in Lightning to check if it is null. If there are no values, return an empty list.
    – sfdcfox
    Aug 13, 2019 at 20:50
  • @sfdcfox you are so right - I'd forgotten to initialise the list in the controller! Works perfectly now :)
    – Irene
    Aug 13, 2019 at 20:53

You must log in to answer this question.

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