5

I have two variables:

public String selectedObject {get; set;} //selectlist
public String selectedField {get; set;}  //selectlist
public String selectedDataType {get; set;}  //text

They are the selected values of my two dynamic picklist for selecting an object and then selecting a specific field and a text field for identifying its data type. I know that I can get the picklist values of a specific field a code like this:

public List<SelectOption> getPicklistValues() {
    List<SelectOption> options = new List<SelectOption>();
    if (selectedDataType=='PICKLIST'){
        Schema.DescribeFieldResult fieldResult = Account.Type.getDescribe();
        List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();
        options.add(new SelectOption('','--None--'));
        for( Schema.PicklistEntry f : ple) {
            options.add(new SelectOption(f.getLabel(), f.getValue()));
        }   
    }

   return options;

}

It can retrieve all the picklist values of the Type field of Account object.

The problem is it is not dynamic (I wanted to replace the Account with the selectedObject value and the Type with the selectedField value and as long as the field type is PICKLIST.

How do I achieve this?

2 Answers 2

9

Instead of Account.Type (a specific field), you can use:

// Get the describe for the object
DescribeSObjectResult objResult = Schema.getGlobalDescribe()
                                    .get(selectedObject).getDescribe();
// Get the field dynamically
DescribeFieldResult fieldResult = objResult.fields.getMap()
                                  .get(selectedField).getDescribe();

At that point, you can get the picklist entries as normal. It's also possible from the field's describe to see if it's a valid picklist; simply call .getType() and see if it's a DisplayType.Picklist, DisplayType.MultiPicklist, or DisplayType.Combobox. If so, you can safely get the picklist values.

0
2

For performances, it's prefered to use Schema.describeSObjects method instead of Schema.getGlobalDescribe.

I did some performance tests and found it faster (time execution) and consume less Heap size.

List<Schema.DescribeSObjectResult> descResults = Schema.describeSObjects(new List<String>{sObjectName});

Example : retrieving recordTypeId by developerName

// Execution time : 31.21 ms
// Heap size 6272
for(Integer i=0;  i<100; i++){
Schema.describeSObjects(new List<String>{'Account'}).get(0).getRecordTypeInfosByDeveloperName().get('Customer_Account').getRecordTypeId();
}

// Execution time : 1156.46 ms
// Heap size 312672
for(Integer i=0;  i<100; i++){
Schema.getGlobalDescribe().get('Account').getDescribe().getRecordTypeInfosByDeveloperName().get('Customer_Account').getRecordTypeId();
}
2
  • 2
    Welcome to Salesforce Stack Exchange (SFSE). Thank you for the contribution. Not that I disbelieve you, but please edit your answer to add some of the numbers that you got when comparing execution time and heap size. Many (dare I say "geeks"?) in this SFSE audience would love to see how much faster and how much smaller.
    – Moonpie
    Oct 2, 2021 at 14:34
  • Hi, thank you for your reply. I added some Numbers related to my performance tests. Oct 2, 2021 at 20:51

You must log in to answer this question.

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