What is Dynamic Apex in Salesforce and how to use it ?

Dynamic apex enables developers to create more flexible applications by providing them with the ability to “Access sObject and field describe information”, “Write Dynamic SOQL Queries”, “Write Dynamic SOSL Queries” and “Dynamic DML”.

It consists of several components such as schema describe, dynamic SOQL, dynamic SOSL, and dynamic DML.

1) Access sObject and field describe information:

Example 1: Get the list of all schema describe, available sobject in salesforce database using Apex (Dynamic Apex):

Map<String, Schema.SObjectType> m = Schema.getGlobalDescribe();

In the above code, it returns map of all Sobjects names or Keys to sObject tokens or values for the standard and custom objects defined in the organization.

Example 2: Get all the fields of sObject using ( Dynamic Apex ):

Map<String, Schema.SObjectType> m  = Schema.getGlobalDescribe() ;
Schema.SObjectType s = m.get('API_Name_Of_SObject') ;
Schema.DescribeSObjectResult r = s.getDescribe() ;
Map<String,Schema.SObjectField> fields = r.fields.getMap() ;

Please also go through the available methods for Schema class:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_schema.htm#apex_System_Schema_getGlobalDescribe

Example 3: Get Record Type Id without SOQL query:

Id recTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get(Rec_Type_Name).getRecordTypeId();

Example 4:  Check whether the Object is Creatable and Insertable for loggedin user:

Schema.SObjectType objInfo = Schema.getGlobalDescribe().get('Account');

Schema.DescribeSobjectResult objResult = objInfo.getDescribe();
System.debug(objResult.isCreateable());
System.debug(objResult.isInsertable());

 

2) Write Dynamic SOQL Queries

SOQL is Salesforce Object Query Language for querying data in the Force.com platform. It is very much similar to SQL. But in SOQL, we can not query all fields from object. Following statement is not allowed in SOQL:
select * from Account;
But there is one workaround to query all fields of Object in SOQL query.
For e:g:  we will use SOQL query to fetch all fields of account object :

String SobjectApiName = 'Account';
Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();

String commaSepratedFields = '';
for(String fieldName : fieldMap.keyset()){
 if(commaSepratedFields == null || commaSepratedFields == ''){
 commaSepratedFields = fieldName;
 }else{
 commaSepratedFields = commaSepratedFields + ', ' + fieldName;
 }
}

string query = 'select ' + commaSepratedFields + ' from ' + SobjectApiName + ' Limit 5';
List<Account> accList = Database.query(query);

 

3) Write Dynamic SOSL Queries

Please refer to the Dynamic SOSL from How to use a SOSL query to search in multiple Salesforce objects?

4) Dynamic DML

Please refer to the Dynamic DML from Apex Developer Guide

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s