Dynamic Apex

Dynamic Apex

1.      Introspecting sObject Information- Applications introspect metedata information and change the behaviour based on what is discovered

a.      Compile time Introspection

//Create a generic sObject variable s 
SObject s = [select id from account limit 1];  
//Verify if that sObject variable is an Account token 
System.assertEquals(s.getSObjectType(), Account.sObjectType); 
//Create a list of generic sObjects 
List<sObject> l = new Account[]{}; 
//Verify if the list of sObjects contains Account tokens 
System.assertEquals(l.getSObjectType(),Account.sObjectType);

Schema.SObjectField F = Account.AccountNumber;

b.      Runtime Introspection

Schema.DescribeFieldResult f = Account.Industry.getDescribe(); 
List<Schema.PicklistEntry> p = f.getPicklistValues();  
List<Schema.ChildRelationship> c = f.getChildRelationships();

2.      Dynamic Queries - The queries are constructed at runtime from within the Apex

Contact c = [select Account.name from Contact]; 
 
sObject c = Database.query('select Account.name from Contact limit 1');
 


Dynamic SOQL and SOSL queries the queries are constructed at runtime from within the APEX.

Comments