Skip to main content
Hello, I am trying to iterate over a list of SObjects, but I'm not sure how to get it to display.

Here is the controller

public with sharing class RandomRecordAudit {

@AuraEnabled(cacheable=true)

public static map<string, string> getAllObjects(){

map<string, string> objectList = new map<string, string>();

for ( Schema.SObjectType o : Schema.getGlobalDescribe().values() )

{

Schema.DescribeSObjectResult objResult = o.getDescribe();

objectList.put(objResult.getName(), objResult.getLabel());

}

return objectList;

}

}

Here is the JS file

import { LightningElement, track } from 'lwc';

import getAllObjects from '@salesforce/apex/RandomRecordAudit.getAllObjects';

export default class SObjectList extends LightningElement {

@track objects;

@track error;

handleLoad() {

getAllObjects()

.then(result => {

this.objects = result;

})

.catch(error => {

this.error = error;

});

}

}

and here is the HTML

<template>

<lightning-card title="ApexImperativeMethod" icon-name="custom:custom63">

<div class="slds-m-around_medium">

<template if:true={objects}>

<template for:each={objects} for:item="object">

<p key={object.Id}>{object.Name}</p>

</template>

</template>

<template if:true={error}>

<p>Error</p>

<c-error-panel errors={error}></c-error-panel>

</template>

</div>

</lightning-card>

</template>

Any help is greatly appreciated, thanks!​​​​​​​
5 risposte
  1. 1 ott 2019, 03:27
    Hi Jacob,

    The below chnages have to be done in Component and JS:

    1. You are call the Apex from handleLoad method in JS but you are not calling this method from any button in Component.  --> So i have used the  connectedCallback to load the data on page load.

    2. I have used the array in JS and lloped the result  to convert the map into a list for template iteration.

    Component:

    <template>

        <lightning-card title="ApexImperativeMethod" icon-name="custom:custom63">

            <div class="slds-m-around_medium">

                <template if:true={objects}>

                    <p>Test</p>

                    <template for:each={objects} for:item="object">

                        <p key={object.key}>{object.value}</p>

                    </template>

                </template>

                <template if:true={error}>

                    <p>Error</p>

                    <c-error-panel errors={error}></c-error-panel>

                </template>

            </div>

        </lightning-card>

    </template>

    ================

    JS :

    /* eslint-disable no-alert */

    import { LightningElement,track } from 'lwc';

    import getAllObjects from '@salesforce/apex/RandomRecordAudit.getAllObjects';

    export default class ForumIterateMap extends LightningElement {

        @track objects = [];

        @track error;

        

            connectedCallback(){

                getAllObjects()

                    .then(result => {

                        //this.objects = result;

                        alert(' Result ==> ' + this.objects);

                        for(let key in result) {

                            // Preventing unexcepted data

                            if (result.hasOwnProperty(key)) { // Filtering the data in the loop

                                this.objects.push({value:result[key], key:key});

                            }

                        }

                    })

                    .catch(error => {

                        this.error = error;

                    });

            

        }

    }

    Thanks,

    Maharajan.C
0/9000