Dynamic Picklist Value RetrievalAutomate the process of picklist values retrieval from the backendSimple Apex class to return a list of stringsFilter and search is not workingColored Picklist Values in VF Pagefault string: No such parameter param defined for the operation, please check the WSDL for the serviceVisualforce dynamic sObject table column value formattingCheck exact characters in custom labels using contains keywordUpdate an object fieldhow to get value from map when wrapper class is the key
Is mountain bike good for long distances?
How to apply a register to a command
What makes an ending "happy"?
How do you say "to hell with everything" in French?
Why are there no wireless switches?
Why did Tony's Arc Reactor do this?
How many attacks exactly do I get combining Dual Wielder feat with Two-Weapon Fighting style?
Is it right to use the ideas of non-winning designers in a design contest?
Did the US Climate Reference Network Show No New Warming Since 2005 in the US?
Template default argument loses its reference type
Where on Earth is it easiest to survive in the wilderness?
Should I tip on the Amtrak train?
Putting future professor position on CV
Why is Sojdlg123aljg a common password?
Why does 8 bit truecolor use only 2 bits for blue?
Friend is very nit picky about side comments I don't intend to be taken too seriously
Python reimplementation of Lost In Space by Tim Hartnell
What quests do you need to stop at before you make an enemy of a faction for each faction?
What exactly is Apple Cider
How can I know what hashing algorithm SQL Server used to decrypt the encrypted data when using the function DECRYPTBYPASSPHRASE?
Relationship between speed and cadence?
Why are UK MPs allowed to abstain (but it counts as a no)?
How can electricity be positive when electrons are negative?
Does the word voltage exist in academic engineering?
Dynamic Picklist Value Retrieval
Automate the process of picklist values retrieval from the backendSimple Apex class to return a list of stringsFilter and search is not workingColored Picklist Values in VF Pagefault string: No such parameter param defined for the operation, please check the WSDL for the serviceVisualforce dynamic sObject table column value formattingCheck exact characters in custom labels using contains keywordUpdate an object fieldhow to get value from map when wrapper class is the key
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I have these two functions that are very similar. They retrieve picklist values for my aura components.
@AuraEnabled(cacheable=true)
public static List<Map<String,String>> getStatusPicklistValues()
List<Map<String,String>> statuses = new List<Map<String,String>>();
for(Schema.PicklistEntry e : Case.Status.getDescribe().getPicklistValues())
if(e.isActive())
statuses.add(
new Map<String, String>
'label' => e.getLabel(),
'value' => e.getValue()
);
return statuses;
@AuraEnabled(cacheable=true)
public static List<Map<String,String>> getCustomFieldPickListValues()
List<Map<String,String>> values = new List<Map<String,String>>();
for(Schema.PicklistEntry e : Case.Custom_Field__c.getDescribe().getPicklistValues())
if(e.isActive())
values.add(
new Map<String, String>
'label' => e.getLabel(),
'value' => e.getValue()
);
return values;
It would be nice if I could have one function like this
@AuraEnabled(cacheable=true)
public static List<Map<String,String>> getPickListValues(String fieldName)
List<Map<String,String>> values = new List<Map<String,String>>();
for(Schema.PicklistEntry e : Case[fieldName].getDescribe().getPicklistValues())
if(e.isActive())
values.add(
new Map<String, String>
'label' => e.getLabel(),
'value' => e.getValue()
);
return values;
Is that possible?
apex aura
add a comment |
I have these two functions that are very similar. They retrieve picklist values for my aura components.
@AuraEnabled(cacheable=true)
public static List<Map<String,String>> getStatusPicklistValues()
List<Map<String,String>> statuses = new List<Map<String,String>>();
for(Schema.PicklistEntry e : Case.Status.getDescribe().getPicklistValues())
if(e.isActive())
statuses.add(
new Map<String, String>
'label' => e.getLabel(),
'value' => e.getValue()
);
return statuses;
@AuraEnabled(cacheable=true)
public static List<Map<String,String>> getCustomFieldPickListValues()
List<Map<String,String>> values = new List<Map<String,String>>();
for(Schema.PicklistEntry e : Case.Custom_Field__c.getDescribe().getPicklistValues())
if(e.isActive())
values.add(
new Map<String, String>
'label' => e.getLabel(),
'value' => e.getValue()
);
return values;
It would be nice if I could have one function like this
@AuraEnabled(cacheable=true)
public static List<Map<String,String>> getPickListValues(String fieldName)
List<Map<String,String>> values = new List<Map<String,String>>();
for(Schema.PicklistEntry e : Case[fieldName].getDescribe().getPicklistValues())
if(e.isActive())
values.add(
new Map<String, String>
'label' => e.getLabel(),
'value' => e.getValue()
);
return values;
Is that possible?
apex aura
1
Have you looked at this: salesforce.stackexchange.com/questions/230261/…
– Arnold Jr.
7 hours ago
add a comment |
I have these two functions that are very similar. They retrieve picklist values for my aura components.
@AuraEnabled(cacheable=true)
public static List<Map<String,String>> getStatusPicklistValues()
List<Map<String,String>> statuses = new List<Map<String,String>>();
for(Schema.PicklistEntry e : Case.Status.getDescribe().getPicklistValues())
if(e.isActive())
statuses.add(
new Map<String, String>
'label' => e.getLabel(),
'value' => e.getValue()
);
return statuses;
@AuraEnabled(cacheable=true)
public static List<Map<String,String>> getCustomFieldPickListValues()
List<Map<String,String>> values = new List<Map<String,String>>();
for(Schema.PicklistEntry e : Case.Custom_Field__c.getDescribe().getPicklistValues())
if(e.isActive())
values.add(
new Map<String, String>
'label' => e.getLabel(),
'value' => e.getValue()
);
return values;
It would be nice if I could have one function like this
@AuraEnabled(cacheable=true)
public static List<Map<String,String>> getPickListValues(String fieldName)
List<Map<String,String>> values = new List<Map<String,String>>();
for(Schema.PicklistEntry e : Case[fieldName].getDescribe().getPicklistValues())
if(e.isActive())
values.add(
new Map<String, String>
'label' => e.getLabel(),
'value' => e.getValue()
);
return values;
Is that possible?
apex aura
I have these two functions that are very similar. They retrieve picklist values for my aura components.
@AuraEnabled(cacheable=true)
public static List<Map<String,String>> getStatusPicklistValues()
List<Map<String,String>> statuses = new List<Map<String,String>>();
for(Schema.PicklistEntry e : Case.Status.getDescribe().getPicklistValues())
if(e.isActive())
statuses.add(
new Map<String, String>
'label' => e.getLabel(),
'value' => e.getValue()
);
return statuses;
@AuraEnabled(cacheable=true)
public static List<Map<String,String>> getCustomFieldPickListValues()
List<Map<String,String>> values = new List<Map<String,String>>();
for(Schema.PicklistEntry e : Case.Custom_Field__c.getDescribe().getPicklistValues())
if(e.isActive())
values.add(
new Map<String, String>
'label' => e.getLabel(),
'value' => e.getValue()
);
return values;
It would be nice if I could have one function like this
@AuraEnabled(cacheable=true)
public static List<Map<String,String>> getPickListValues(String fieldName)
List<Map<String,String>> values = new List<Map<String,String>>();
for(Schema.PicklistEntry e : Case[fieldName].getDescribe().getPicklistValues())
if(e.isActive())
values.add(
new Map<String, String>
'label' => e.getLabel(),
'value' => e.getValue()
);
return values;
Is that possible?
apex aura
apex aura
edited 8 hours ago
Tyler Zika
asked 8 hours ago
Tyler ZikaTyler Zika
9372 gold badges12 silver badges34 bronze badges
9372 gold badges12 silver badges34 bronze badges
1
Have you looked at this: salesforce.stackexchange.com/questions/230261/…
– Arnold Jr.
7 hours ago
add a comment |
1
Have you looked at this: salesforce.stackexchange.com/questions/230261/…
– Arnold Jr.
7 hours ago
1
1
Have you looked at this: salesforce.stackexchange.com/questions/230261/…
– Arnold Jr.
7 hours ago
Have you looked at this: salesforce.stackexchange.com/questions/230261/…
– Arnold Jr.
7 hours ago
add a comment |
3 Answers
3
active
oldest
votes
You can use below method where you can pass objectName and comma separated picklist fields (even single field is fine) to get map of object and its picklist values of value and label:
@AuraEnabled
public static Map<String, Map<String,String>> getPicklistValues(String objectName, String picklistFields)
if(string.isNotBlank(picklistFields))
Map<String, Map<String,String>> picklistValuesMap = new Map<String, Map<String,String>>();
for(String picklistField : picklistFields.split(','))
picklistField = picklistField.trim();
Schema.DescribeFieldResult stagesFR = Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap().get(picklistField).getDescribe();
List<Schema.PicklistEntry> stagesPle = stagesFR.getPicklistValues();
Map<String,String> valuesList = new Map<String,String>();
for( Schema.PicklistEntry s : stagesPle)
valuesList.put(s.getValue(), s.getLabel());
picklistValuesMap.put(picklistField, valuesList);
return picklistValuesMap;
return null;
So, for getting picklist options for 2 fields - Type and Active__c from Account object:
System.debug(JSON.serialize(poc.getPicklistValues('Account', 'Type,Active__c')));
OUTPUT:
"Active__c":
"Yes": "Yes",
"No": "No"
,
"Type":
"Other": "Other translated",
"Technology Partner": "Technology Translated",
"Installation Partner": "Installation translated",
"Channel Partner / Reseller": "Channel Partner Translated",
"Customer - Channel": "Customer Channel Translated",
"Customer - Direct": "Direct Translated",
"Prospect": "Prospect Translated"
Notice that we get label to value for each picklist field.
Also note that you can set comma separated fields as parameter or else just list of fields api strings by doing small change in code to just iterate over list of strings of fields. I just made it comma separated because whether you pass single or multiple fields, its fine (in case of list, you need to pass list of 1 field which could be confusing some times).
AMAZING. Can we use JSON2Apex to deserialize the JSON? or does it make the overall code more complicated?
– Arnold Jr.
7 hours ago
I just serialised it for understanding (in anonymous apex), you can use it as is
– salesforce-sas
7 hours ago
add a comment |
Yep, it's possible.
@AuraEnabled(cacheable=true)
public static List<Map<String,String>> getPickListValues(String fieldName)
List<Map<String,String>> values = new List<Map<String,String>>();
for(Schema.PicklistEntry e : SObjectType.Case.fields.getMap().get(fieldName).getDescribe().getPicklistValues())
if(e.isActive())
values.add(
new Map<String, String>
'label' => e.getLabel(),
'value' => e.getValue()
);
return values;
Just to hightlight SObjectType.Case.fields.getMap().get(fieldName). Usually, you can use SObjectType prefix to do more dynamic staff. And there is also option to use
String sobjectName = 'Case';
String fieldName = 'Status';
List<PicklistEntry> picklistValues = Schema.getGlobalDescribe().get(sobjectName).getDescribe().fields.getMap().get(fieldName).getDescribe().getPicklistValues()
If you want to go one step further in the abstraction. Play around with this in Anonymous execute. Because sometimes it's really hard to understand which code will compile and which not))
Good luck.
P.S. I would recommend to add checks for isAccessible() and check in general in fieldMap contains your fieldName.
add a comment |
I appreciate all the answers. This is what works for my code. All the other answer appear to work as well.
@AuraEnabled(cacheable=true)
public static List<Map<String,String>> getPicklistValues(String objectName, String fieldName)
Boolean hasDefault = false;
List<Map<String,String>> values = new List<Map<String,String>>();
for(PicklistEntry e: ((SObject)(Type.forName('Schema.'+objectName).newInstance())).getSObjectType()
.getDescribe().fields.getMap().get(fieldName).getDescribe().getPicklistValues())
if(e.isActive())
values.add(
new Map<String, String>
'label' => e.getLabel(),
'value' => e.getValue()
);
if(e.isDefaultValue())
hasDefault = true;
// we need to add a blank picklist value if a field has no default value,
// because that means the field is blank in the UI.
if(!hasDefault)
values.add(0,
new Map<String, String>
'label' => '',
'value' => null
);
return values;
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "459"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f276234%2fdynamic-picklist-value-retrieval%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use below method where you can pass objectName and comma separated picklist fields (even single field is fine) to get map of object and its picklist values of value and label:
@AuraEnabled
public static Map<String, Map<String,String>> getPicklistValues(String objectName, String picklistFields)
if(string.isNotBlank(picklistFields))
Map<String, Map<String,String>> picklistValuesMap = new Map<String, Map<String,String>>();
for(String picklistField : picklistFields.split(','))
picklistField = picklistField.trim();
Schema.DescribeFieldResult stagesFR = Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap().get(picklistField).getDescribe();
List<Schema.PicklistEntry> stagesPle = stagesFR.getPicklistValues();
Map<String,String> valuesList = new Map<String,String>();
for( Schema.PicklistEntry s : stagesPle)
valuesList.put(s.getValue(), s.getLabel());
picklistValuesMap.put(picklistField, valuesList);
return picklistValuesMap;
return null;
So, for getting picklist options for 2 fields - Type and Active__c from Account object:
System.debug(JSON.serialize(poc.getPicklistValues('Account', 'Type,Active__c')));
OUTPUT:
"Active__c":
"Yes": "Yes",
"No": "No"
,
"Type":
"Other": "Other translated",
"Technology Partner": "Technology Translated",
"Installation Partner": "Installation translated",
"Channel Partner / Reseller": "Channel Partner Translated",
"Customer - Channel": "Customer Channel Translated",
"Customer - Direct": "Direct Translated",
"Prospect": "Prospect Translated"
Notice that we get label to value for each picklist field.
Also note that you can set comma separated fields as parameter or else just list of fields api strings by doing small change in code to just iterate over list of strings of fields. I just made it comma separated because whether you pass single or multiple fields, its fine (in case of list, you need to pass list of 1 field which could be confusing some times).
AMAZING. Can we use JSON2Apex to deserialize the JSON? or does it make the overall code more complicated?
– Arnold Jr.
7 hours ago
I just serialised it for understanding (in anonymous apex), you can use it as is
– salesforce-sas
7 hours ago
add a comment |
You can use below method where you can pass objectName and comma separated picklist fields (even single field is fine) to get map of object and its picklist values of value and label:
@AuraEnabled
public static Map<String, Map<String,String>> getPicklistValues(String objectName, String picklistFields)
if(string.isNotBlank(picklistFields))
Map<String, Map<String,String>> picklistValuesMap = new Map<String, Map<String,String>>();
for(String picklistField : picklistFields.split(','))
picklistField = picklistField.trim();
Schema.DescribeFieldResult stagesFR = Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap().get(picklistField).getDescribe();
List<Schema.PicklistEntry> stagesPle = stagesFR.getPicklistValues();
Map<String,String> valuesList = new Map<String,String>();
for( Schema.PicklistEntry s : stagesPle)
valuesList.put(s.getValue(), s.getLabel());
picklistValuesMap.put(picklistField, valuesList);
return picklistValuesMap;
return null;
So, for getting picklist options for 2 fields - Type and Active__c from Account object:
System.debug(JSON.serialize(poc.getPicklistValues('Account', 'Type,Active__c')));
OUTPUT:
"Active__c":
"Yes": "Yes",
"No": "No"
,
"Type":
"Other": "Other translated",
"Technology Partner": "Technology Translated",
"Installation Partner": "Installation translated",
"Channel Partner / Reseller": "Channel Partner Translated",
"Customer - Channel": "Customer Channel Translated",
"Customer - Direct": "Direct Translated",
"Prospect": "Prospect Translated"
Notice that we get label to value for each picklist field.
Also note that you can set comma separated fields as parameter or else just list of fields api strings by doing small change in code to just iterate over list of strings of fields. I just made it comma separated because whether you pass single or multiple fields, its fine (in case of list, you need to pass list of 1 field which could be confusing some times).
AMAZING. Can we use JSON2Apex to deserialize the JSON? or does it make the overall code more complicated?
– Arnold Jr.
7 hours ago
I just serialised it for understanding (in anonymous apex), you can use it as is
– salesforce-sas
7 hours ago
add a comment |
You can use below method where you can pass objectName and comma separated picklist fields (even single field is fine) to get map of object and its picklist values of value and label:
@AuraEnabled
public static Map<String, Map<String,String>> getPicklistValues(String objectName, String picklistFields)
if(string.isNotBlank(picklistFields))
Map<String, Map<String,String>> picklistValuesMap = new Map<String, Map<String,String>>();
for(String picklistField : picklistFields.split(','))
picklistField = picklistField.trim();
Schema.DescribeFieldResult stagesFR = Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap().get(picklistField).getDescribe();
List<Schema.PicklistEntry> stagesPle = stagesFR.getPicklistValues();
Map<String,String> valuesList = new Map<String,String>();
for( Schema.PicklistEntry s : stagesPle)
valuesList.put(s.getValue(), s.getLabel());
picklistValuesMap.put(picklistField, valuesList);
return picklistValuesMap;
return null;
So, for getting picklist options for 2 fields - Type and Active__c from Account object:
System.debug(JSON.serialize(poc.getPicklistValues('Account', 'Type,Active__c')));
OUTPUT:
"Active__c":
"Yes": "Yes",
"No": "No"
,
"Type":
"Other": "Other translated",
"Technology Partner": "Technology Translated",
"Installation Partner": "Installation translated",
"Channel Partner / Reseller": "Channel Partner Translated",
"Customer - Channel": "Customer Channel Translated",
"Customer - Direct": "Direct Translated",
"Prospect": "Prospect Translated"
Notice that we get label to value for each picklist field.
Also note that you can set comma separated fields as parameter or else just list of fields api strings by doing small change in code to just iterate over list of strings of fields. I just made it comma separated because whether you pass single or multiple fields, its fine (in case of list, you need to pass list of 1 field which could be confusing some times).
You can use below method where you can pass objectName and comma separated picklist fields (even single field is fine) to get map of object and its picklist values of value and label:
@AuraEnabled
public static Map<String, Map<String,String>> getPicklistValues(String objectName, String picklistFields)
if(string.isNotBlank(picklistFields))
Map<String, Map<String,String>> picklistValuesMap = new Map<String, Map<String,String>>();
for(String picklistField : picklistFields.split(','))
picklistField = picklistField.trim();
Schema.DescribeFieldResult stagesFR = Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap().get(picklistField).getDescribe();
List<Schema.PicklistEntry> stagesPle = stagesFR.getPicklistValues();
Map<String,String> valuesList = new Map<String,String>();
for( Schema.PicklistEntry s : stagesPle)
valuesList.put(s.getValue(), s.getLabel());
picklistValuesMap.put(picklistField, valuesList);
return picklistValuesMap;
return null;
So, for getting picklist options for 2 fields - Type and Active__c from Account object:
System.debug(JSON.serialize(poc.getPicklistValues('Account', 'Type,Active__c')));
OUTPUT:
"Active__c":
"Yes": "Yes",
"No": "No"
,
"Type":
"Other": "Other translated",
"Technology Partner": "Technology Translated",
"Installation Partner": "Installation translated",
"Channel Partner / Reseller": "Channel Partner Translated",
"Customer - Channel": "Customer Channel Translated",
"Customer - Direct": "Direct Translated",
"Prospect": "Prospect Translated"
Notice that we get label to value for each picklist field.
Also note that you can set comma separated fields as parameter or else just list of fields api strings by doing small change in code to just iterate over list of strings of fields. I just made it comma separated because whether you pass single or multiple fields, its fine (in case of list, you need to pass list of 1 field which could be confusing some times).
edited 7 hours ago
answered 7 hours ago
salesforce-sassalesforce-sas
9,0861 gold badge3 silver badges26 bronze badges
9,0861 gold badge3 silver badges26 bronze badges
AMAZING. Can we use JSON2Apex to deserialize the JSON? or does it make the overall code more complicated?
– Arnold Jr.
7 hours ago
I just serialised it for understanding (in anonymous apex), you can use it as is
– salesforce-sas
7 hours ago
add a comment |
AMAZING. Can we use JSON2Apex to deserialize the JSON? or does it make the overall code more complicated?
– Arnold Jr.
7 hours ago
I just serialised it for understanding (in anonymous apex), you can use it as is
– salesforce-sas
7 hours ago
AMAZING. Can we use JSON2Apex to deserialize the JSON? or does it make the overall code more complicated?
– Arnold Jr.
7 hours ago
AMAZING. Can we use JSON2Apex to deserialize the JSON? or does it make the overall code more complicated?
– Arnold Jr.
7 hours ago
I just serialised it for understanding (in anonymous apex), you can use it as is
– salesforce-sas
7 hours ago
I just serialised it for understanding (in anonymous apex), you can use it as is
– salesforce-sas
7 hours ago
add a comment |
Yep, it's possible.
@AuraEnabled(cacheable=true)
public static List<Map<String,String>> getPickListValues(String fieldName)
List<Map<String,String>> values = new List<Map<String,String>>();
for(Schema.PicklistEntry e : SObjectType.Case.fields.getMap().get(fieldName).getDescribe().getPicklistValues())
if(e.isActive())
values.add(
new Map<String, String>
'label' => e.getLabel(),
'value' => e.getValue()
);
return values;
Just to hightlight SObjectType.Case.fields.getMap().get(fieldName). Usually, you can use SObjectType prefix to do more dynamic staff. And there is also option to use
String sobjectName = 'Case';
String fieldName = 'Status';
List<PicklistEntry> picklistValues = Schema.getGlobalDescribe().get(sobjectName).getDescribe().fields.getMap().get(fieldName).getDescribe().getPicklistValues()
If you want to go one step further in the abstraction. Play around with this in Anonymous execute. Because sometimes it's really hard to understand which code will compile and which not))
Good luck.
P.S. I would recommend to add checks for isAccessible() and check in general in fieldMap contains your fieldName.
add a comment |
Yep, it's possible.
@AuraEnabled(cacheable=true)
public static List<Map<String,String>> getPickListValues(String fieldName)
List<Map<String,String>> values = new List<Map<String,String>>();
for(Schema.PicklistEntry e : SObjectType.Case.fields.getMap().get(fieldName).getDescribe().getPicklistValues())
if(e.isActive())
values.add(
new Map<String, String>
'label' => e.getLabel(),
'value' => e.getValue()
);
return values;
Just to hightlight SObjectType.Case.fields.getMap().get(fieldName). Usually, you can use SObjectType prefix to do more dynamic staff. And there is also option to use
String sobjectName = 'Case';
String fieldName = 'Status';
List<PicklistEntry> picklistValues = Schema.getGlobalDescribe().get(sobjectName).getDescribe().fields.getMap().get(fieldName).getDescribe().getPicklistValues()
If you want to go one step further in the abstraction. Play around with this in Anonymous execute. Because sometimes it's really hard to understand which code will compile and which not))
Good luck.
P.S. I would recommend to add checks for isAccessible() and check in general in fieldMap contains your fieldName.
add a comment |
Yep, it's possible.
@AuraEnabled(cacheable=true)
public static List<Map<String,String>> getPickListValues(String fieldName)
List<Map<String,String>> values = new List<Map<String,String>>();
for(Schema.PicklistEntry e : SObjectType.Case.fields.getMap().get(fieldName).getDescribe().getPicklistValues())
if(e.isActive())
values.add(
new Map<String, String>
'label' => e.getLabel(),
'value' => e.getValue()
);
return values;
Just to hightlight SObjectType.Case.fields.getMap().get(fieldName). Usually, you can use SObjectType prefix to do more dynamic staff. And there is also option to use
String sobjectName = 'Case';
String fieldName = 'Status';
List<PicklistEntry> picklistValues = Schema.getGlobalDescribe().get(sobjectName).getDescribe().fields.getMap().get(fieldName).getDescribe().getPicklistValues()
If you want to go one step further in the abstraction. Play around with this in Anonymous execute. Because sometimes it's really hard to understand which code will compile and which not))
Good luck.
P.S. I would recommend to add checks for isAccessible() and check in general in fieldMap contains your fieldName.
Yep, it's possible.
@AuraEnabled(cacheable=true)
public static List<Map<String,String>> getPickListValues(String fieldName)
List<Map<String,String>> values = new List<Map<String,String>>();
for(Schema.PicklistEntry e : SObjectType.Case.fields.getMap().get(fieldName).getDescribe().getPicklistValues())
if(e.isActive())
values.add(
new Map<String, String>
'label' => e.getLabel(),
'value' => e.getValue()
);
return values;
Just to hightlight SObjectType.Case.fields.getMap().get(fieldName). Usually, you can use SObjectType prefix to do more dynamic staff. And there is also option to use
String sobjectName = 'Case';
String fieldName = 'Status';
List<PicklistEntry> picklistValues = Schema.getGlobalDescribe().get(sobjectName).getDescribe().fields.getMap().get(fieldName).getDescribe().getPicklistValues()
If you want to go one step further in the abstraction. Play around with this in Anonymous execute. Because sometimes it's really hard to understand which code will compile and which not))
Good luck.
P.S. I would recommend to add checks for isAccessible() and check in general in fieldMap contains your fieldName.
edited 7 hours ago
answered 7 hours ago
ytiqytiq
6324 silver badges18 bronze badges
6324 silver badges18 bronze badges
add a comment |
add a comment |
I appreciate all the answers. This is what works for my code. All the other answer appear to work as well.
@AuraEnabled(cacheable=true)
public static List<Map<String,String>> getPicklistValues(String objectName, String fieldName)
Boolean hasDefault = false;
List<Map<String,String>> values = new List<Map<String,String>>();
for(PicklistEntry e: ((SObject)(Type.forName('Schema.'+objectName).newInstance())).getSObjectType()
.getDescribe().fields.getMap().get(fieldName).getDescribe().getPicklistValues())
if(e.isActive())
values.add(
new Map<String, String>
'label' => e.getLabel(),
'value' => e.getValue()
);
if(e.isDefaultValue())
hasDefault = true;
// we need to add a blank picklist value if a field has no default value,
// because that means the field is blank in the UI.
if(!hasDefault)
values.add(0,
new Map<String, String>
'label' => '',
'value' => null
);
return values;
add a comment |
I appreciate all the answers. This is what works for my code. All the other answer appear to work as well.
@AuraEnabled(cacheable=true)
public static List<Map<String,String>> getPicklistValues(String objectName, String fieldName)
Boolean hasDefault = false;
List<Map<String,String>> values = new List<Map<String,String>>();
for(PicklistEntry e: ((SObject)(Type.forName('Schema.'+objectName).newInstance())).getSObjectType()
.getDescribe().fields.getMap().get(fieldName).getDescribe().getPicklistValues())
if(e.isActive())
values.add(
new Map<String, String>
'label' => e.getLabel(),
'value' => e.getValue()
);
if(e.isDefaultValue())
hasDefault = true;
// we need to add a blank picklist value if a field has no default value,
// because that means the field is blank in the UI.
if(!hasDefault)
values.add(0,
new Map<String, String>
'label' => '',
'value' => null
);
return values;
add a comment |
I appreciate all the answers. This is what works for my code. All the other answer appear to work as well.
@AuraEnabled(cacheable=true)
public static List<Map<String,String>> getPicklistValues(String objectName, String fieldName)
Boolean hasDefault = false;
List<Map<String,String>> values = new List<Map<String,String>>();
for(PicklistEntry e: ((SObject)(Type.forName('Schema.'+objectName).newInstance())).getSObjectType()
.getDescribe().fields.getMap().get(fieldName).getDescribe().getPicklistValues())
if(e.isActive())
values.add(
new Map<String, String>
'label' => e.getLabel(),
'value' => e.getValue()
);
if(e.isDefaultValue())
hasDefault = true;
// we need to add a blank picklist value if a field has no default value,
// because that means the field is blank in the UI.
if(!hasDefault)
values.add(0,
new Map<String, String>
'label' => '',
'value' => null
);
return values;
I appreciate all the answers. This is what works for my code. All the other answer appear to work as well.
@AuraEnabled(cacheable=true)
public static List<Map<String,String>> getPicklistValues(String objectName, String fieldName)
Boolean hasDefault = false;
List<Map<String,String>> values = new List<Map<String,String>>();
for(PicklistEntry e: ((SObject)(Type.forName('Schema.'+objectName).newInstance())).getSObjectType()
.getDescribe().fields.getMap().get(fieldName).getDescribe().getPicklistValues())
if(e.isActive())
values.add(
new Map<String, String>
'label' => e.getLabel(),
'value' => e.getValue()
);
if(e.isDefaultValue())
hasDefault = true;
// we need to add a blank picklist value if a field has no default value,
// because that means the field is blank in the UI.
if(!hasDefault)
values.add(0,
new Map<String, String>
'label' => '',
'value' => null
);
return values;
answered 46 mins ago
Tyler ZikaTyler Zika
9372 gold badges12 silver badges34 bronze badges
9372 gold badges12 silver badges34 bronze badges
add a comment |
add a comment |
Thanks for contributing an answer to Salesforce Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f276234%2fdynamic-picklist-value-retrieval%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Have you looked at this: salesforce.stackexchange.com/questions/230261/…
– Arnold Jr.
7 hours ago