0

I use <lightning:select><option> pair tags with an sobject as a list.

The sobject is populated from a call to an Apex class method which returns the list:

var action = component.get("c.getRecords");       
action.setCallback(this, function(response) {
    var state = response.getState();
    if (state === "SUCCESS") {
        var opts = response.getReturnValue();  
        component.set("v.options", opts);
    }
    else {
        console.log("Failed with state: " + state);
    }
});
$A.enqueueAction(action);   

The fields queried are the ID and the Name of the sobjects. The following code is supposed to:

  • Show the name in the list box
  • Set the Id as the value to return

    <lightning:select name="mySelect" label="Select a color:" aura:id="mySelect" value="{!v.selectedValue}" onchange="{!c.onChange}">
         <aura:iteration items="{!v.options}" var="item">
            <option text="{!item.Name}" value="{!item.id}" />
        </aura:iteration>
    </lightning:select>
    
    <p>
        Selection value : <ui:outputText value="{!v.outputText}" />
    </p>
    

If I take a look at the generated code, there is no value attribute in the tags.

<div class="slds-select_container" data-aura-rendered-by="14:0">
    <select class="slds-select" id="5:0" data-aura-rendered-by="15:0" name="mySelect">
        <option data-aura-rendered-by="1:6;a">Record 1</option>
    </select>
</div>

Consequently, I can't have the ID back with the expression component.find("mySelect").get("v.value");

Th value attribute is generated correctly when the options attribute/variable type is set to List. The problem happens when the Type is changed to an SObject.

Is it normal behavior? What should be done to have the ID set in the value attribute of the option tag when the list is of type SObject?

Thanks for your help

3
  • 1
    How is your options declared currently - SObject[]? Have you tried changing the id to Id (note the uppercase I) and see if that works?
    – Jayant Das
    Jan 6, 2019 at 17:39
  • Hi Jayant, You're right : it works with Id, and not id or ID. Finally, it looks like the first letter must always be Upper case. Just need to take a look in the response content with the javascript debugger to be sure of the exact case to adopt.
    – fredbe
    Jan 7, 2019 at 10:16
  • I added more details as an answer. Hope it helps.
    – Jayant Das
    Jan 7, 2019 at 12:12

1 Answer 1

1

The issue here is with the usage of the field name ({!item.id}) as why the value attribute is not getting populated.

You will need to use {!item.Id} (note the uppercase I in Id). If you print the values returned from Apex, you will notice that all the field names are deserialized with the first letter capitalized. So when you use it on your component with lowercase, and JS being case sensitive, the value attribute never gets populated. Thus you will need to use the name of the fields as returned.

Even if you use lowercase field names in SOQL, the result returned will always have the first letter capitalized for the field names. So whenever you need to refer the field names in your JS or component, just capitalize the first letter and that will work.

You must log in to answer this question.

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