Created
April 24, 2025 00:04
-
-
Save robertStrunk/a4e781bbb267c3b1392e631662a27491 to your computer and use it in GitHub Desktop.
LWC PicklistViewer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public with sharing class DynamicPicklistController { | |
@AuraEnabled(cacheable=true) | |
public static List<String> getSObjectNames() { | |
List<String> sObjects = new List<String>(); | |
for (Schema.SObjectType sObjType : Schema.getGlobalDescribe().values()) { | |
String name = sObjType.getDescribe().getName(); | |
if (!sObjType.getDescribe().isCustomSetting() && | |
!sObjType.getDescribe().isDeprecatedAndHidden() && | |
sObjType.getDescribe().isCreateable()) { | |
sObjects.add(name); | |
} | |
} | |
sObjects.sort(); | |
return sObjects; | |
} | |
@AuraEnabled(cacheable=true) | |
public static List<String> getPicklistFields(String sObjectName) { | |
Map<String, Schema.SObjectField> fieldsMap = Schema.getGlobalDescribe().get(sObjectName).getDescribe().fields.getMap(); | |
List<String> picklistFields = new List<String>(); | |
for (String fieldName : fieldsMap.keySet()) { | |
Schema.DescribeFieldResult fieldDescribe = fieldsMap.get(fieldName).getDescribe(); | |
if (fieldDescribe.getType() == Schema.DisplayType.Picklist) { | |
picklistFields.add(fieldDescribe.getName()); | |
} | |
} | |
return picklistFields; | |
} | |
@AuraEnabled(cacheable=true) | |
public static List<Object> getPicklistValues(String sObjectName, String fieldName) { | |
Schema.DescribeFieldResult fieldDescribe = | |
Schema.getGlobalDescribe().get(sObjectName).getDescribe().fields.getMap().get(fieldName).getDescribe(); | |
List<Object> values = new List<Object>(); | |
for (Schema.PicklistEntry entry : fieldDescribe.getPicklistValues()) { | |
Map<String, Object> picklistEntryMap = new Map<String, Object>{ | |
'label' => entry.getLabel(), | |
'value' => entry.getValue(), | |
'isActive' => entry.isActive(), | |
'isDefaultValue' => entry.isDefaultValue() | |
}; | |
values.add(picklistEntryMap); | |
} | |
return values; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<lightning-card title="Dynamic Picklist Viewer"> | |
<div class="slds-p-around_medium"> | |
<lightning-combobox | |
name="sobject" | |
label="Select SObject" | |
value={selectedObject} | |
options={sObjectOptions} | |
onchange={handleSObjectChange}> | |
</lightning-combobox> | |
<template if:true={picklistFieldOptions}> | |
<lightning-combobox | |
name="picklist" | |
label="Select Picklist Field" | |
value={selectedPicklistField} | |
options={picklistFieldOptions} | |
onchange={handlePicklistChange}> | |
</lightning-combobox> | |
</template> | |
<template if:true={picklistValues}> | |
<lightning-datatable | |
key-field="value" | |
data={picklistValues} | |
columns={columns}> | |
</lightning-datatable> | |
</template> | |
</div> | |
</lightning-card> | |
</template> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { LightningElement, track } from 'lwc'; | |
import getSObjectNames from '@salesforce/apex/DynamicPicklistController.getSObjectNames'; | |
import getPicklistFields from '@salesforce/apex/DynamicPicklistController.getPicklistFields'; | |
import getPicklistValues from '@salesforce/apex/DynamicPicklistController.getPicklistValues'; | |
export default class PicklistViewer extends LightningElement { | |
@track sObjectOptions = []; | |
@track picklistFieldOptions = []; | |
@track picklistValues = []; | |
@track selectedObject; | |
@track selectedPicklistField; | |
columns = [ | |
{ label: 'Label', fieldName: 'label' }, | |
{ label: 'Value', fieldName: 'value' }, | |
{ label: 'Is Active', fieldName: 'isActive' }, | |
{ label: 'Is Default Value', fieldName: 'isDefaultValue' }, | |
]; | |
connectedCallback() { | |
getSObjectNames().then(result => { | |
this.sObjectOptions = result.map(name => ({ | |
label: name, | |
value: name | |
})); | |
}); | |
} | |
handleSObjectChange(event) { | |
this.selectedObject = event.detail.value; | |
this.selectedPicklistField = null; | |
this.picklistValues = []; | |
getPicklistFields({ sObjectName: this.selectedObject }).then(fields => { | |
this.picklistFieldOptions = fields.map(f => ({ | |
label: f, | |
value: f | |
})); | |
}); | |
} | |
handlePicklistChange(event) { | |
this.selectedPicklistField = event.detail.value; | |
getPicklistValues({ | |
sObjectName: this.selectedObject, | |
fieldName: this.selectedPicklistField | |
}).then(values => { | |
this.picklistValues = values | |
}); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"> | |
<apiVersion>63.0</apiVersion> | |
<isExposed>true</isExposed> | |
<targets> | |
<target>lightning__AppPage</target> | |
<target>lightning__RecordPage</target> | |
<target>lightning__HomePage</target> | |
</targets> | |
</LightningComponentBundle> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment