[SalesForce] lwc combo box is not loading any values

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>

Best Answer

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];
}
Related Topic