3

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...

4

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>
  • obj.value.name doesn't work. Have you tested it? – Jayant Das Feb 27 at 16:51
  • @JayantDas, yes I used this and working fine. – Imran Qureshi Feb 27 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 Feb 27 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 Feb 28 at 7:39
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);
    }
}
  • What is the return type of the List here? Do you mind posting your complete code? – Jayant Das Feb 28 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..! – Anshul Agrawal Feb 28 at 8:00
  • 1
    OP's question was around custom classes whereas in your case you are returning standard SObject. – Jayant Das Feb 28 at 13:28

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.