0

Hello guys I want to show in a lightning-combobox values from a SObject that has two important fields: Value__c and Key__c.

My apex code looks like this one:

@AuraEnabled(cacheable=true)
public static List<WrapperField> getValues (){
    List<WrapperField> wrapperToLWC = new List<WrapperField>();
    List<SObject> listSystem =   [SELECT Id, Table__c, Value__c, Key__c FROM SObject WHERE Table__c =: 'CN'];
    if(listSystem.isEmpty()){
        throw new QX_Exception('Could not find the vales');
    }else{
        for(SObject iRow: listSystem){
            WrapperField iWrapper = new WrapperField();
            iWrapper.value = iRow.Value__c;
            iWrapper.key = iRow.Key__c;
            wrapperToLWC.add(iWrapper);
        }
        return wrapperToLWC;
    }
}

public class WrapperField{
    @AuraEnabled public String value{get;set;}
    @AuraEnabled public String key{get;set;}
}

And in my js code I have this:

picklistToUser = [];
    connectedCallback(){
        getValues().then(result =>{
            if(result != null){
                for(let iRow in result){
                    this.picklistToUser.push({label: result[iRow].value, value: result[iRow].key})
                    console.log('valuesss '+ JSON.stringify(this.picklistToUser));
                }
            }
        })
    }

the console.log actually brings me the data but in the .html does not appear.

   <lightning-combobox  label="MyField" placeholder="Select value" options={picklistToUser} onchange={handleMyField}></lightning-combobox>

I have 629 record to show. So, I saw this was not working so I console.log a picklist value that came directly from the system and brought me this structure:

{"attributes":null,
 "label":"label1a",
 "validFor":[],"value":"S"},
{"attributes":null,"label":"Label2","validFor":[],"value":"N"}, 
{"attributes":null,"label":"Label3","validFor":[],"value":"A"}

So I did something similar to check if that worked, but it didn't:

  this.picklistToUser.push({"attributes":null,"label": result[iRow].value, "value": result[iRow].key,"validFor":[]})

Could anybody help me with this? I have no clue I do not know if this is important, but I have a js code exclusively for the picklist values brought from the SObject (the js code above is in this file). However I have an other js file where I am working in a form so I only include the js file that has all of the picklist and works but exactly for this field is not. Thanks

1 Answer 1

0

If picklistToUser is not using @track, then it won't work correctly, because the changes won't be picked up. For this reason, I always recommend avoiding the use of Array.prototype.push in favor of Array.prototype.map:

getValues().then((result) => {
  this.picklistToUser = result.map(({ value: label, key: value }) => ({
    label,
    value,
  }));
});

It's also worth noting that the wrapper using value and key is just a point of confusion; it would be much better to use label and value directly.

You must log in to answer this question.

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