4

I'm struggling trying to figure out how to iterate over an array of objects returned by an Apex method to print object name + label as key.

The structure of the object is the following:

public class SalesforceObject {
    public String name;
    public String label;
}

JS Controller is the following:

import { LightningElement, wire } from 'lwc';
import getSObjectList from '@salesforce/apex/ReportsAppController.getSObjectList';
export default class SObjectList extends LightningElement {
    @wire(getSObjectList) objectList; 
}

And my template is the following one:

<template>
    <template if:true={objectList.data}>
        <template for:each={objectList.data} for:item="object">
                <p key={object.Label}>{object.Name}</p>
        </template>
    </template>
</template>

Already tried to capitalize and lower-case Label & Name words, but still is not working...

3 Answers 3

7

Make the following changes:-

Apex: Class

public class SalesforceObject{
    @AuraEnabled
    public String name{set;get;}
    @AuraEnabled
    public String label{set;get;}
}

Lightning Web Component:-

<template iterator:obj={objectList}>
    <p key={obj.value.name}>{obj.value.label}</p>
</template>
5
  • obj.value.name doesn't work. Have you tested it?
    – Jayant Das
    Commented Feb 27, 2019 at 16:51
  • @JayantDas, yes I used this and working fine. Commented Feb 27, 2019 at 16:55
  • Interesting, it doesn't work for me. I copied the code that you have in here and it threw an error in after render.
    – Jayant Das
    Commented Feb 27, 2019 at 17:00
  • Worked like a charm. Needed to put @AuraEnabled in SalesforceObject inner class and to use .value to access label/name properties.
    – Dahn
    Commented Feb 28, 2019 at 7:39
  • I needed to iterate over data, i.e., <template iterator:obj={objectList.data}>. Commented Sep 12, 2019 at 19:07
2

With a quick test, seems @AuraEnabled as was case with Lightning Aura Component, the annotation is still required to access properties of custom class on a LWC as well.

So to access the properties, you will need to annotate the properties with @AuraEnabled.

public class SalesforceObject {
    @AuraEnabled public String name;
    @AuraEnabled public String label;
}

And use the same case on the HTML:

<p key={object.label}>{object.name}</p>
2

For me Iterating over objects works perfectly fine without Annotating the properties in Apex, i am using the @wire like this:

The data received is List sent as response from the Apex. Used @track topNewsServer to assign and iterate in html

Apex method: -

@AuraEnabled(cacheable=true)
public static Map<String, Object> getTopNewsData(String filter) 
{
  Map<String, Object> response = new Map<String, Object>();
  //performs logic here to build
  List<Account> accessibleAccounts = [SELECT
                Id, Name
        FROM Account
        ORDER BY LastModifiedDate DESC LIMIT 100];
        response.put('accounts', accessibleAccounts);
        // do something else...
        response.put('values', timelineResponse);
        return response;
}



@wire(getTopNewsData, {filter: '$topNewsKey'})
wiredTopNewsResponse({ error, data }) {
    if (data) {
        this.loading = false;
        if(data!=null )this.topNewsServer = data.values;
    } else if (error){
        this.errorToastTopNews(error.body.message);
    }
}
3
  • What is the return type of the List here? Do you mind posting your complete code?
    – Jayant Das
    Commented Feb 28, 2019 at 1:22
  • Updated my code snippet.Hope it helps now. from Apex its passed as Object, which can be anything, a string, a list of sObjects..! Commented Feb 28, 2019 at 8:00
  • 1
    OP's question was around custom classes whereas in your case you are returning standard SObject.
    – Jayant Das
    Commented Feb 28, 2019 at 13:28

You must log in to answer this question.

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