Dynamic Apex
- johnsontitus
- May 6, 2020
- 2 min read
Updated: May 6, 2020
It gives the ability to:
Describe information provides metadata information about sObject and field properties.
For example, the describe information for an sObject includes
whether that type of sObject supports operations like create or undelete,
the sObject's name and label,
the sObject's fields
and child objects, and so on.
The describe information for a field includes
whether the field has a default value,
whether it is a calculated field,
the type of the field, and so on.
Note that describe information provides information about objects in an organization, not individual records.
Understanding Apex Describe Information:
public class AccessDataModel { public static void describeResultOfObject(){ // Create a new account as the generic type sObject sObject s = new Account(); // Verify that the generic sObject is an Account sObject // Both will return result - Account
//both return token for Account using getsObjectType method and sObjectType
//member variable System.assert(s.getsObjectType() == Account.sObjectType); // Get the sObject describe result for the Account object Schema.DescribeSObjectResult dsr = Account.sObjectType.getDescribe(); //On the describe result, the getSObjectType returns the token for sObject Schema.SObjectType acct = dsr.getSObjectType(); // Get the field describe result for the 'Name' field on the Account object Schema.DescribeFieldResult dfr = Schema.sObjectType.Account.fields.Name; ////On the describe result, the getSObjectField returns the token for the Name field Schema.SObjectField nme = dfr.getSobjectField(); // Verify that the field token is the token for the Name field on an Account object System.assert(dfr.getSObjectField() == Account.Name); // Get the field describe result from the token dfr = dfr.getSObjectField().getDescribe(); } }
Explaination:
Apex provides two data structures and a method for sObject and field describe information:
Token—a lightweight reference to an sObject or a field. This is used for token describes.
The describeSObjects method—a method in the Schema class that performs describes on one or more sObject types.
Describe result—an object of type Schema.DescribeSObjectResult that contains all the describe properties for the sObject or field.
Use case:
The OpportunityStatusHistory__c custom object fields are named like Prospecting__c, Qualification__c, etc. The field names are equal to the Stage Name’s in Opportunity object.
Whenever a new Opportunity record is created or updated with a particular stage, then we need to count the stage history details in “OpportunityStatusHistory” object. For example, if we create an opportunity with Stage “Prospecting”, then OpportunityStatusHistory object record should be created with the corresponding Prospecting__c field value set as 1.
If we changed the above opportunity stage to “Qualification”, then corresponding OpportunityStatusHistory object field Qualification__c value to be set as 1. If I go to backward stage i.e. “Prospecting”, then related Prospecting__c field in OpportunityStatusHistory record should get updated as 2.
Solution:
trigger trgOpportunityStatus on Opportunity (before insert) {
//On the describe result, the getSObjectType returns the token for sObject Schema.SObjectType oppStat = OpportunityStatusHistory__c.getSObjectType();
Map<String, Schema.SObjectField> opportunityStatusDescribe = oppStat.getDescribe().fields.getMap(); Map<String, String> opportunityStatusFieldMap = new Map<String, String>(); for(Schema.SObjectField currentField : acctTypes.values()){ Schema.DescribeFieldResult fieldResult = currentField.getDescribe(); opportunityStatusFieldMap.put(fieldResult.getLabel(),fieldResult.getName()); }
OpportunityStatusHistory__c statusHistory = new OpportunityStatusHistory__c();
for(Opportunity currentOpp : Trigger.New){
//put(fieldName, value)
statusHistory.put(opportunityStatusFieldMap.get(currentOpp.Stage__c), 1);
} }

Comments