0

I need to populate my lightning-dual-listbox with the fields of selected object. My apex class returns the list of fields, I have fetched the values in this.objectFields but they are not getting populated in dual-listbox.

Apex class

   @AuraEnabled(cacheable=true)
    public static List<String> getObjectFields(String objectName){
        system.debug(objectName);
        SObjectType objectType = Schema.getGlobalDescribe().get((objectName));
        Map<String,Schema.SObjectField> mfields = objectType.getDescribe().fields.getMap();

        List<String> sObjectDetailsList = new List<String>();
        for(String s : mfields.keySet()){
            sObjectDetailsList.add(String.valueOf(mfields.get(s)));
        }
        return sObjectDetailsList;
    }

JS file

@track objectFields = [];
 getObjectFields({
            objectName: this.selectedObject
        })
        .then(result => {
            var fields = result;
            console.log('fields : '+fields);
            this.objectFields = fields;
            console.log('this.objectFields : '+this.objectFields);

        })
        .catch(error => {
            this.error = error;
            console.log('error ===>: ' + error.body.message);
        });

HTML File

<lightning-dual-listbox name="Fields"
                            label="Select Fields"
                            source-label="Available"
                            selected-label="Selected"
                            field-level-help="Select your preferred fields"
                            options={objectFields}
                            onchange={handleChange}></lightning-dual-listbox>
1

If you check the documenation, you might notice that each object in the array of options contains the following properties: label and value:

{ label: 'English', value: 'en' }

so, you should make sure that each of your results contains these in order to properly appear in the dual-listbox options.

0

Try store the using label, value format in objectFields, as below

@track objectFields = [];
 getObjectFields({
            objectName: this.selectedObject
        })
        .then(result => {
            result.forEach(element=> this.objectFields.push({
                value: element.value,
                label: element.value});
         })
        .catch(error => {
            this.error = error;
            console.log('error ===>: ' + error.body.message);
        });

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.