0

I need to display object label instead of API name in the combobox. How to access label of object dynamically? Below is the scrrenshot and code!

enter image description here

.JS

getCustomObjects({
        })
            .then(result => {
                var i;
                for (i = 0; i < result.length; i++) {
                    var item = {
                        label: result[i],
                        value: result[i]
                    };
                    this.customObjectOptions.push(item)
                    console.log('this.customObjectOptions : ' + JSON.stringify(this.customObjectOptions));
                }
            })
            .catch(error => {
                this.error = error;
                console.log('error ===>: ' + error.body.message);
            });

.html

 <template if:true={showCustomComboBox}>
                    <lightning-combobox name="CustomObject" label="Select Custom Object:" aria-setsize="5"
                        options={customObjectOptions} onchange={handleChange}>
                    </lightning-combobox>
                </template>
0

You can use the getObjectInfos function of lightning/uiObjectInfoApi module. https://developer.salesforce.com/docs/component-library/documentation/en/lwc/lwc.reference_wire_adapters_object_infos

This will return an array of results where you can get the Label of the objects.

I quickly modified a bit the example of the page to match your requirement but you will need to modify it a bit more still.

To fire the wire you would have to modifty the binded variable with the ApiNames of the objects you want.

import { LightningElement, wire } from 'lwc';
import { getObjectInfos } from 'lightning/uiObjectInfoApi';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';
import OPPORTUNITY_OBJECT from '@salesforce/schema/Opportunity';

export default class WireGetObjectInfo extends LightningElement {
    objectApiNames = [ACCOUNT_OBJECT, OPPORTUNITY_OBJECT];

@wire(getObjectInfos, { objectApiNames: '$objectApiNames' })
wireHandler({ data, error }) {
   if (data) {
     for (i = 0; i < data.results.length; i++) {
                var item = {
                    label: data.results[i].label,
                    value: data.results[i].apiName
                };
                this.customObjectOptions.push(item)
      
     }
   }
}



}
2
  • What if I don't want to use @wire method? – Indresh Bhargav Feb 4 at 13:45
  • You can return from the apex a List<Map<String,String> or List<WrapperClass> returning the values builded in apex, for example: [ { 'apiName', 'Account', 'Label', 'Account', }, { 'apiName', 'Custom_Object__c', 'Label', 'Custom Object', } ] This should not being difficult to build as you can use schema class, then in your JS just change the iteration to use: label: result[i].Label, value: result[i].apiName – ivan Feb 5 at 11:37

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.