Posts

Showing posts with the label Describe

Get Picklist Values Using Dynamic Apex

Image
  The picklist field name and object are the two parameters that the code snippet below requires. We can read either the Picklist label or the value based on these two inputs. String objectName = 'Account'; String fieldName ='Industry'; Schema.SObjectType s = Schema.getGlobalDescribe().get(objectName) ; Schema.DescribeSObjectResult r = s.getDescribe() ; Map<String,Schema.SObjectField> fields = r.fields.getMap() ; Schema.DescribeFieldResult fieldResult = fields.get(fieldName).getDescribe(); List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues(); for( Schema.PicklistEntry pickListVal : ple){ System.debug(pickListVal.getLabel() +' ---- '+pickListVal.getValue()); }  

Dynamic Apex in Salesforce

Dynamic Apex in Salesforce refers to the ability to construct and execute code dynamically at runtime, rather than having it defined statically at compile time. This feature allows developers to build more flexible and adaptable solutions that can respond to changing requirements or conditions during program execution. Dynamic Apex enables developers to create more flexible applications by providing them with the ability to: Access sObject and field describe information- Metadata information about sObject and field properties includes details about operations supported by sObject types (such as create or undelete), name and label of sObjects, fields and child objects. Field information contains details like default value presence, calculated field status, and field type Apex provides two data structures and a method for sObject and field describe information: Token —a lightweight, serializable reference to an sObject or a field that is validated at compile time. This is used for token...