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
Nishant Shrivastava 30Nishant Shrivastava 30 

Lightening Web component combobox is not working

Hi,
I am trying to create a lightening web component combobox but it's not working. Please check the code

Javascript:
import { LightningElement, api, wire, track } from 'lwc';
import methodName from '@salesforce/apex/MoveAttachment.methodName';
export default class attachmentMove extends LightningElement {
    @track record;
    @track result;
    @track error;
    @wire(methodName)sobjectName;
    wiredsobjectName({error, data}){
        if(data){
            if(data.length>0){
            this.arrays.array.forEach(element => {
            this.record.push ({label: record[i].Id, value: record[i].Name });
            console.log('check=====>', this.result);
            });
        }
            this.result = record;
            this.error = undefined;
           
        } else if (error){
            this.error = error;
            this.result = undefined;
        }
    }
    get options() {
        console.log('option--->', this.sobjectName.data);
        return this.sobjectName.data;
    }
    get values(){
        return this.result;
    }
    handleChange(event) {
        this.value = event.detail.value;
    }
}
/*
    */
 

HTML:


<template>
    <lightning-card title="Combo Box_Type Values" icon-name="custom:custom67">
    <template  if:true={sobjectName.data}>
    <lightning-combobox
            name="List of Object"
            label="List of Object"
            value={result}
            placeholder="Select Object"
            options = {options}
            onchange={handleChange} >
        </lightning-combobox>
    <p>Selected value is: {options}</p>
</template>
</lightning-card>
</template>


If use get option then it shows blank value in dropdown, if i use this.result. then it does not work. say this.result is undefined and also this.result. Please help me to fix it.
Thanks and regards
Nishant Shrivastava

mukesh guptamukesh gupta
Hi Nishat,

Please use below code:-
 
import { LightningElement, api, wire, track } from 'lwc';
import methodName from '@salesforce/apex/MoveAttachment.methodName';
export default class attachmentMove extends LightningElement {
   @track TypeOptions;
    @wire(methodName)
    wiredsobjectName({error, data}){
       let options = [];
	    if (data) {
            try {
		   for (var key in data) {
				// Here key will have index of list of records starting from 0,1,2,....
				options.push({ label: data[key].Name, value: data[key].Id  });

				// Here Name and Id are fields from sObject list.
			}
			this.TypeOptions = options;
		
		 } catch (error) {
                console.error('check error here', error);
            }
        } else if (error) {
            console.error('check error here', error);
        }
		}
	   
    get optionList() {
        return this.TypeOptions.length()>0 ? true : false ;
    }
   
    handleChange(event) {
        this.value = event.detail.value;
    }
}
 
<template>
    <lightning-card title="Combo Box_Type Values" icon-name="custom:custom67">
    <template  if:true={optionList}>
    <lightning-combobox
            name="List of Object"
            label="List of Object"
            value={value}
            placeholder="Select Object"
            options = {TypeOptions}
            onchange={handleChange} >
        </lightning-combobox>
    <p>Selected value is: {TypeOptions}</p>
</template>
</lightning-card>
</template>

if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 
CharuDuttCharuDutt
Hii Nishant
Try Below Code
HTML
 <lightning-combobox class="slds-m-around_small" name="List Of Objects" label="List Of Objects" placeholder="List Of Objects" value={value} onchange={handleChange} options={Options}>
        </lightning-combobox>



JS
import { LightningElement, api, wire, track } from 'lwc';
import getAllObjectsfrom '@salesforce/apex/MoveAttachment.getAllObjects';
export default class attachmentMove extends LightningElement {
data;
value;
get Options() {
        return this.data;
    }

    @wire(getAllObjects)
    wiredClass({data, error}) {
        if(data) {
            for(let key in data) {
               
                if (data[key]) {
                    this.data = [...this.data ,{value: key , label: data[key]}]; 
                }
            }
        }
        else if(error) {
           console.log(error);
        }
    }



handleChange(event){
     alert(event.detail.value);
     this.value = event.detail.value;
        
}
}



Apex


 @AuraEnabled(cacheable=true)
    public static Map<String,String> getAllObjects(){
        Map<String,String> objectMap = new Map<String,String>();
        for(Schema.SObjectType objTyp : Schema.getGlobalDescribe().Values()){
           String name = objTyp.getDescribe().getName();
           String label = objTyp.getDescribe().getLabel();
           if(!name.containsignorecase('history') && !name.containsignorecase('tag') &&
                !name.containsignorecase('share') && !name.containsignorecase('feed') && 
                !name.containsignorecase('group') && !name.containsignorecase('public') &&
                !objTyp.getDescribe().isCustomSetting() && objTyp.getDescribe().isCreateable() &&
                objTyp.getDescribe().getRecordTypeInfos().size() > 0){      
                objectMap.put(name,label);
                    
           }
        }
        System.debug(objectMap);
        if(objectMap != null && objectMap.size() > 0){
            return objectMap;   
        }else{
            return null;
        }
    }
Please Mark It As Best Answer If It Helps
Thank You!
Cathal DotnetCathal Dotnet

When I attempted CharuDutt's solution, I got an error:

[this.data is not iterable]]

 

Not sure if I am the only person who this happened to, but there you go.