0

I'm trying to get all currency field type of a specific object and display it in lightning-combobox but it does not work. When I execute this method with anonymous window, I got the result I want. But when I call it from LWC, it does not respond or show any error.

==> I'm not sure this problem caused because I'm having 2 dropdown lists or not?

enter image description here

HTML code:

       <lightning-combobox name="fieldList" 
            label="Currency Fields" 
            onchange={handleGetSummarizedOptions} 
            options={lstOfSummarizedFields} 
            placeholder="Select Field">
        </lightning-combobox>
        <lightning-combobox name="fieldList" 
            label="Picklist Fields" 
            onchange={handleGetPicklistFieldsOptions} 
            options={lstOfPicklistFields} 
            placeholder="Select Field">
        </lightning-combobox>

Apex code:

@AuraEnabled(cacheable=true)
    public static Map<String,String> getPicklistTypeFields(String strObjectName) {
        Map<String, Schema.SObjectType> detail = Schema.getGlobalDescribe();
        Map<String,String> mapOfPicklistTypeFields = new  Map<String,String>();
        for(Schema.SObjectField fields :detail.get(strObjectName).getDescribe().fields.getMap().Values()) {
            If(fields.getDescribe().getType() == Schema.DisplayType.PICKLIST) { 
                mapOfPicklistTypeFields.put(fields.getDescribe().getLabel(), fields.getDescribe().getName());
            }
        }
        return mapOfPicklistTypeFields;
    }

@AuraEnabled(cacheable=true)
    public static Map<String,String> getCurrencyTypeFields(String strObjectName) {
        Map<String, Schema.SObjectType> detail = Schema.getGlobalDescribe();
        Map<String,String> mapOfCurrencyTypeFields = new  Map<String,String>();
        for(Schema.SObjectField fields :detail.get(strObjectName).getDescribe().fields.getMap().Values()) {
            If(fields.getDescribe().getType() == Schema.DisplayType.CURRENCY) { 
                mapOfCurrencyTypeFields.put(fields.getDescribe().getLabel() , fields.getDescribe().getName());
            }
        }
        return mapOfCurrencyTypeFields;
    }

1/ I tried wired Apex:

    @api objectApiName;

    lstOfPicklistFields = [];
    wireAllPickListRes = ''
    @wire(getPicklistTypeFields, ({ strObjectName : '$objectApiName' }))
    wireAllPickList(result) {
        this.wireAllPickListRes = result;
        let {error, data} = result;
        if(data) {
            this.lstOfPicklistFields = [];
            for (let key in data) {
                this.lstOfPicklistFields.push({ label: key, value: data[key] });
            }
        }
        if(error) {
            console.log('Error in getting picklist fields');
        }
    }

    lstOfSummarizedFields = [];
    wireAllCurrencyRes = ''
    @wire(getCurrencyTypeFields, ({ strObjectName : this.objectApiName }))
    wireAllCurrency(result) {
        this.wireAllCurrencyRes = result;
        console.log('Currency res', this.wireAllCurrencyRes.data);
        let {error, data} = result;
        if(data) {
            this.lstOfSummarizedFields = [];
            for (let key in data) {
                this.lstOfSummarizedFields.push({ label: key, value: data[key] });
            }
        }
        console.log('Currency field',this.lstOfSummarizedFields);
        if(error) {
            console.log('Error in getting picklist fields');
        }
    }

I noticed this error returned when I wired it enter image description here

2/ I also try to call it from async/await (a function will invoke it)

@track lstOfSummarizedFields = [];
    async getPicklistValuesForSelectedPicklistField() {
        await getCurrencyTypeFields({ strObjectName : this.objectApiName })
        .then((result) => {
            if(result) {
                this.lstOfSummarizedFields = [];
                for (let key in result) {
                    this.lstOfSummarizedFields.push({ label: key, value: data[key] });
                }
            }
        })
        .catch((error) => {
            console.log('Error in getting currency values')
          })
    }

Thank you so much!

1 Answer 1

0

The wired method didn't work because you didn't make it reactive.

@wire(getCurrencyTypeFields, { strObjectName : "$objectApiName" })
wireAllCurrency(result) {

Use the "$variableName" syntax to make a property reactive.


For the second attempt, you shouldn't mix await and then. Use either Promise and then, or async and await.

async getPicklistValuesForSelectedPicklistField() {
  try {
    let result = await getCurrencyTypeFields({ strObjectName: this.objectApiName });
    this.lstOfSummarizedFields = Object.entries(result).map(
      ([label, value]) => ({ label, value })
    );
  } catch(e) {
    console.log(e);
  }
}
1
  • Hi @sfdcfox, thank you for your answer. When I changed to reactive property, it works!
    – Pam
    Commented Aug 15, 2023 at 2:46

You must log in to answer this question.

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