1

Looking for some examples of how to pass values from Apex List,generated in Apex class, to LWC picklist. My picklist is :

<lightning-combobox name="progress"
    label="Object"
    value={value}
    placeholder="-Select-"
    options={options}
    onchange={handleChange} >
</lightning-combobox>

Apex method:

 @AuraEnabled
    public static List<String> getObjectNames(){
        Map<String,Schema.SObjectType> AllObjectMap=new Map<String,Schema.SObjectType>();
        AllObjectMap=Schema.getGlobalDescribe();
        List<String> l = new List<String>(AllObjectMap.keySet()); 
        return l;
    }

js file for LWC

import getObjectNames from '@salesforce/apex/CustomLookupController.getObjectNames';
//...here goes some code
get options(){
        //?
    }
2
  • Can you post your code until where you have tried and let us know where you are stuck?
    – samdev
    Commented Dec 3, 2020 at 20:15
  • edited the post
    – Masha
    Commented Dec 3, 2020 at 20:23

2 Answers 2

3

I would recommend you go thru LWC Trailhead Modules.

Below is the Sample Code - your JS File Should Look like.

import { LightningElement, track } from "lwc";
import getObjectNames from "@salesforce/apex/CustomLookupController.getObjectNames";

export default class componentName extends LightningElement {
  @track options;

  connectedCallBack() {
    this.getOptions();
  }

  getOptions() {
    getObjectNames({})
      .then((result) => {
         let options = [];
        if (result) {
          result.forEach(r => {
            options.push({
              label: r,
              value: r,
            });
          });
        }
        this.options = options;
      })
      .catch((error) => {
        // handle Error
      });
  }
  
}
0

Here is another way to write the javascript:

HTML:

 <lightning-combobox class="yourPicklistClass"
      label="Your label"
      name="yourPicklistName"
      options={picklistOptions}
      value={value}
      dropdown-alignment="auto"
      onchange={handleYourChange}
      placeholder={yourPlaceholder}>

JS:

import { LightningElement, track } from "lwc";
import getObjectNames from "@salesforce/apex/CustomLookupController.getObjectNames";

export default class YourComponentName extends LightningElement {
  @track picklistOptions= [];

   connectedCallBack() {
     this.getYourPicklist();
   }

    async getYourPicklist() {
        let theOptions = await getObjectNames();
        for (const [i, j] of Object.entries(theOptions)) {
            this.picklistOptions.push({label: i, value: j});
        }
    }

Kind regards!)

You must log in to answer this question.

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