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;








1















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?










share|improve this question





















  • 1





    Have you looked at this: salesforce.stackexchange.com/questions/230261/…

    – Arnold Jr.
    7 hours ago

















1















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?










share|improve this question





















  • 1





    Have you looked at this: salesforce.stackexchange.com/questions/230261/…

    – Arnold Jr.
    7 hours ago













1












1








1


1






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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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












  • 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










3 Answers
3






active

oldest

votes


















4
















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).






share|improve this answer



























  • 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


















1
















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.






share|improve this answer


































    0
















    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;






    share|improve this answer



























      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
      );



      );














      draft saved

      draft discarded
















      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









      4
















      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).






      share|improve this answer



























      • 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















      4
















      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).






      share|improve this answer



























      • 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













      4














      4










      4









      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).






      share|improve this answer















      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).







      share|improve this answer














      share|improve this answer



      share|improve this answer








      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

















      • 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













      1
















      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.






      share|improve this answer































        1
















        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.






        share|improve this answer





























          1














          1










          1









          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.






          share|improve this answer















          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.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited 7 hours ago

























          answered 7 hours ago









          ytiqytiq

          6324 silver badges18 bronze badges




          6324 silver badges18 bronze badges
























              0
















              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;






              share|improve this answer





























                0
















                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;






                share|improve this answer



























                  0














                  0










                  0









                  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;






                  share|improve this answer













                  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;







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 46 mins ago









                  Tyler ZikaTyler Zika

                  9372 gold badges12 silver badges34 bronze badges




                  9372 gold badges12 silver badges34 bronze badges































                      draft saved

                      draft discarded















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Canceling a color specificationRandomly assigning color to Graphics3D objects?Default color for Filling in Mathematica 9Coloring specific elements of sets with a prime modified order in an array plotHow to pick a color differing significantly from the colors already in a given color list?Detection of the text colorColor numbers based on their valueCan color schemes for use with ColorData include opacity specification?My dynamic color schemes

                      Invision Community Contents History See also References External links Navigation menuProprietaryinvisioncommunity.comIPS Community ForumsIPS Community Forumsthis blog entry"License Changes, IP.Board 3.4, and the Future""Interview -- Matt Mecham of Ibforums""CEO Invision Power Board, Matt Mecham Is a Liar, Thief!"IPB License Explanation 1.3, 1.3.1, 2.0, and 2.1ArchivedSecurity Fixes, Updates And Enhancements For IPB 1.3.1Archived"New Demo Accounts - Invision Power Services"the original"New Default Skin"the original"Invision Power Board 3.0.0 and Applications Released"the original"Archived copy"the original"Perpetual licenses being done away with""Release Notes - Invision Power Services""Introducing: IPS Community Suite 4!"Invision Community Release Notes

                      In Tikz, how to set a node's label alignment to the left?Rotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?Numerical conditional within tikz keys?TikZ/ERD: node (=Entity) label on the insideLine up nested tikz enviroments or how to get rid of themVertically align a tikzpicture and forestDrawing tikz line in the margin for multiple pagesLongtable, contained tikz, padding, custom columns, and an alignment issueTikZ: define arrow starting position based on style and format node labelAlign node name in Tikz