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
sksfdc221sksfdc221 

LWC to display salesforce objects as a picklist onclick

I have a requirement where I need to display list of salesforce objects as a picklist when a radio button is clicked.
Being new to LWC development, I could not figure out what is the right approach to do so. I have come across this code (https://developer.salesforce.com/docs/component-library/tools/playground/QWa-zfwxW/60/edit) from Salesforce articles where we can display radio buttons but as per my scenario, i need to display list of all salesforce objects as picklist when a radio button option is selected.
Can anyone please suggest the right approach to do so.
AbhishekAbhishek (Salesforce Developers) 
Hi,

You need an AuraEnabled class that returns the list of sObjects. And onclick or most probably onchange of your radio button should call that apex method so that you can use the response list as a picklist valueSet.


===============================
@AuraEnabled(cacheable=true)
    public static Map<String, String> getSObjects() {        
        Map<String, String> sObjectNamebyLabel = new Map<String, String>();
        for(SObjectType objectType : Schema.getGlobalDescribe().values()) {
            DescribeSObjectResult sObjectDescribe = objectType.getDescribe();
            if(sObjectDescribe.isAccessible() && sObjectDescribe.isQueryable()) {
                sObjectNamebyLabel.put(sObjectDescribe.getLabel(),sObjectDescribe.getName());
            }
        }        
        return sObjectNamebyLabel;
    }
================================

on the lwc JS side you can call getSObjectOptions whenever or where ever you want to

====================================

getSObjectOptions() { getSObjects() .then((response) => { // assign to your picklist or lookup value set }) .catch((error) => { // toast your error }); }

If I were you I would use dynamic lookup component instead of a picklist. There will be a lot of sObject and picklist is not the right element type to display that amount of options. Check below git repo for custom lwc lookup.

https://github.com/pozil/sfdc-ui-lookup-lwc


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


Thanks.
David Zhu 🔥David Zhu 🔥
You may take the following example as reference.

1. add a apex method to get all objects
@auraenabeld (cacheable=true)
public static list<string> getObjectList()
{
    ................
     Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); 
       for (string objName : gd.keyset())
      {
         system.debug(objName);
      }

     .................
}

2. Add the following to lwc javascript
import getObjectListfrom '@salesforce/apex/YourController.getObjectList';
@track displayList;
@wire (getObjectListfrom ,{}) allObjectList;

in radio button click event, set
displayList = true; //or false based on the requirement

3.On Hmtl file
Display allObjectList
<template if:true={displayList}>
   <template for:each={allObjectList} for:item="objName">
      <li key={objName.value}> {objName.value></li>
    </template>
</template>
 
sksfdc221sksfdc221
@David Zhu, while saving the apex class, I'm getting the error as "Missing return statement required return type: List<String>". Can you please suggest any changes
David Zhu 🔥David Zhu 🔥
The reference is just a mockup, you have to do some modifications by providing the correct return type.