function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Connor CainConnor Cain 

Lighting-Combobox returning null when selecting a value

hello everyone,
We have a Lightning Web Component with 3 Comboboxes. I built out the first one using the code from the Lighting Web Component resource page and it works great. I then copied and pasted the code (while changing the value names and labels as to not cause an error)  but the two new Combo Boxes arent working. when selecting a value the ComboBox returns a null value rather than the initial ' ' value and doesnt select the value I wanted to select. 
Syed Insha Jawaid 2Syed Insha Jawaid 2
Hi Conor

Please share the code snippet.

Cheers!!!
Deepali KulshresthaDeepali Kulshrestha
Hi Connor,

lightning:combobox is an input element that enables single selection from a list of options. The result of the selection is displayed as the value of the input.A 

widget that provides an input field that is read-only, accompanied with a dropdown list of selectable options.In this example, once the user selects the Department 

from the contact, it will get all the contacts and display as the table . Please refer to the following code as it may be helpful in solving your problem:

Apex Class:
public class ContactsController {
    @AuraEnabled
    public static List<String> getDepartment () {
        Set<String> uniqDepartment = new Set<String>();
        List<Contact> results=[Select Id, Department from Contact];
        for(Contact c : results){
            if(c.Department!=null){
                uniqDepartment.add(c.Department); 
            }
        }
        List<String> finalResult =  new List<String>();
        finalResult.addAll(uniqDepartment); 
        finalResult.sort();
        return  finalResult;
    }
    
    @AuraEnabled
    public static List<Contact> getContactsByDept (String deptName) {
        List<Contact> results=[Select Id,FirstName,LastName,Email,Department from Contact where Department=:deptName];
        return  results;
    }
        
}

Lightning Component:
<aura:component controller="ContactsController">
    <aura:attribute name="statusOptions" type="List" default="[]"/>
    <aura:attribute name="contacts" type="Contact[]" default="[]"/>
    
    <aura:handler name="init" value="{! this }" action="{! c.loadOptions }"/>
    <lightning:combobox aura:id="selectItem" name="status" label="Status"
                        placeholder="Choose Status"
                        value="new"
                        required="true"
                        dropdownAlignment="right"
                        variant="standard"
                        messageWhenValueMissing="You Must Select the Value"
                        onchange="{!c.handleOptionSelected}"
                        options="{!v.statusOptions}"/>
    
    <table class="slds-table slds-table--bordered">
        <thead>
            <th>
                LastName 
            </th>
            
            <th>
                FirstName
            </th>
            <th>
                Email
            </th>
        </thead>
        <tbody>
            <aura:iteration items="{!v.contacts}" var="row">
                <tr>
                    <td>
                        {!row.LastName}
                    </td>
                    <td>
                        {!row.FirstName}
                    </td>
                    <td>
                        {!row.Email}
                    </td>
                </tr>
            </aura:iteration>
        </tbody>
    </table>
    
    
</aura:component>

Controller:
({
    loadOptions: function (cmp, event, helper) {
        var options = [
        ];
        var action = cmp.get("c.getDepartment");
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                var arr = response.getReturnValue() ;
                arr.forEach(function(element) {
                    options.push({ value: element, label: element });
                });
                cmp.set("v.statusOptions", options);
                
            }
            
            
        });
        $A.enqueueAction(action);
        
    },
    handleOptionSelected: function (cmp, event) {
        var action = cmp.get("c.getContactsByDept");
        action.setParams({deptName:event.getParam("value")});
        action.setCallback(this, function(response) {
            var state = response.getState();
            if (state === "SUCCESS") {
                cmp.set("v.contacts" ,  response.getReturnValue() ) ; 
            }
        });
        $A.enqueueAction(action); 
        
    }
})

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
 
Connor CainConnor Cain
here are snipits of my code: 
Combobox 1 is working fine but 2 and 3 are passing null values into my apex code
HTML:
/*inside tab 1*/
<lightning-combobox 
      name="Catagory1" 
      value={value1} 
      placeholder="Select a Category"
      options={options1} 
      onchange={handleChange1}>
</lightning-combobox>

/*Inside tab 2*/
<lightning-combobox 
      name="Catagory2"
      value={value2}
      placeholder="Select a Category"
      options={options2}
      onchange={handleChange2}>
</lightning-combobox>

/*inside tab 3*/
<lightning-combobox 
      name="Catagory3" 
      value={value3} 
      placeholder="Select a Category"
      options={options3} 
      onchange={handleChange3}>
</lightning-combobox>
JS
/*inside default class*/
@track value1 = '';
@track value2 = '';
@track value3 = '';
@wire(ApexClass, {RecId: '$recordId', stage: 'dataTable1', searchval: '$buttonClick', Category: '$value1'})
deliver1;
@wire(ApexClass, {RecId: '$recordId', stage: 'dataTable2', searchval: '$buttonClick', Category: '$value2'})
deliver2;
@wire(ApexClass, {RecId: '$recordId', stage: 'dataTable3', searchval: '$buttonClick', Category: '$value3'})
deliver3;
get options1(){
        return [
            { label: 'Option 1', value: 'Option1' },
            { label: 'Option 2', value: 'Option2' },
            { label: 'Option 3', value: 'Option3' },
        ];
    }
    get options2(){
        return [
            { label: 'Option 1', value: 'Option1' },
            { label: 'Option 2', value: 'Option2' },
            { label: 'Option 3', value: 'Option3' },
        ];
    }
    get options3(){
        return [
            { label: 'Option 1', value: 'Option1' },
            { label: 'Option 2', value: 'Option2' },
            { label: 'Option 3', value: 'Option3' },
        ];
    }
    handleChange1(event) {
        this.value1 = event.detail.value;
        this.buttonClick = 'Not';    //to override some buttons when refreshing the query
    }
    handleChange2(event) {
        this.value2 = event.detail.value;
        this.buttonClick = 'Not';    //to override some buttons when refreshing the query
    }
    handleChange3(event) {
        this.value3 = event.detail.value;
        this.buttonClick = 'Not';   //to override some buttons when refreshing the query
    }