0

I am pulling values form apex method to display in the lightning combo box. I get 500 internal server error from the console log and i see the method name and class name are correct. Not sure where i am missing.

import { LightningElement, wire, track } from 'lwc';
import getDivisions from '@salesforce/apex/TrafficReportController.getDivisions';

export default class TrafficHistoryReport extends LightningElement {
    @track divisionvalue = '';   
    @track Divisionvalues = [];
    @wire(getDivisions) 
    ListDivisions({data,error}){
        if(data){ 
            for(let i=0;i < data.length;i++){
                this.Divisionvalues.push({
                    label: data[i],
                    value: data[i]
                });
            }
            //this.divisionoption = this.Divisionvalues;
        }
        else if (error){
            window.consolde.log(error);
        }
    }

    get divisionoption() {
        //console.log(this.Divisionvalues);
        return this.Divisionvalues;
    }

    handleChange(event){
        event.preventDefault();
        console.log('value'+event.target.value)
    }

}

Apex:

public with sharing class TrafficReportController {
    public TrafficReportController() {

    }

    @AuraEnabled (cacheable=true)    
    public static List<String> getDivisions(){
       List<Division__c> LstDivisions = [Select id, Name from Division__c];
       List<String> Lststrings = new Lststrings();
       for(Division__c Div:LstDivisions){
           Lststrings.add(Div.Name);
       }
       return Lststrings;
    }
}

HTML:

<template>
    <div>Traffic History Report</div>
    <div>
        <lightning-combobox name="Divisions" label="Divisions" placeholder="Select a Division" value={divisionvalue} options={divisionoption} onchange={handlechange}></lightning-combobox>
    </div>

</template>
2
  • I'm sure there's more to the error message aside from a simple 500 internal error. Check the Chrome console (dig into the object spit out) and set up user debugs / trace flags for the dev console for logs coming out of the dev console. Commented Dec 5, 2019 at 18:48
  • 1
    I had the List<String> Lststrings = new Lststrings(); defined wrong and VScode did not error. Also i was trying to test in local development environment. I tried to deployed in my org and the error came up. I was able to fix this issue. Now a new issue is duplicate item in the combo box. Need to figure iout how to save Label and ID in value so that each dropdown is unique.
    – Rajesh
    Commented Dec 5, 2019 at 19:41

1 Answer 1

1

You need to restart the list every time you get a new set of data (wire events can trigger more than once).

@wire(getDivisions) 
ListDivisions({data,error}) {
    if(data){ 
        this.Divisionvalues = data.map(record => ({ label: record.Name, value: record.Id }));
    } else {
        window.consolde.log(error);
    }
}

You should be using ID values instead of the name for the options:

@AuraEnabled (cacheable=true)    
public static Division__c[] getDivisions(){
   return [Select id, Name from Division__c];
}
4
  • Thanks. I am planning to have a dynamic combo box using this combo box value. Can you please provide me some reference on how to do it?
    – Rajesh
    Commented Dec 6, 2019 at 0:40
  • Debug log shows the query is executed in the backend but in the front end, i see the list populated only if i navigate to different tab and come back. on load the list does not show any value.
    – Rajesh
    Commented Dec 10, 2019 at 17:43
  • @Rajesh Do not use "push", or it may not work. Note specifically I use Array.map instead to convert the values to a new Array and assign it to the attribute. This triggers a rerender and allows the values to show immediately.
    – sfdcfox
    Commented Dec 10, 2019 at 19:13
  • it worked. Thanks.
    – Rajesh
    Commented Dec 10, 2019 at 21:20

You must log in to answer this question.

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