function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SakthidasanSakthidasan 

when should we use dynamic apex and dynamic soql

I'm leanring dynamic apex.it can be useful for create flexible application.but when we should we use dynamic apex.please explain real time example
Best Answer chosen by Sakthidasan
@Karanraj@Karanraj
Below example scenario you can able to make use of sObject and field describe (Dynamic Apex Class)

1. You want to export the all the fields from an object.
With the help of dynamic apex, you can able to retrieve all the fields and form a dynamic SOQL query and display the field in visualforce and able to export it into CSV or PDF file. In future, if the user adds any new field in the object, you don't have to modify your apex code or visualforce page to display. SObject and field describe will get the fields dynamically from the mentioned object.

2. Checking field level security 
If you want to enforce the field level security in your visualforce page [custom controller ] you have use the sObject and field describe information to check the field level security for the running user 
if (!Schema.sObjectType.Contact.fields.Name.isAccessible()){
          return '';
}
Check this article to know more details about enforcing CRUD and FLS security in visualforce page - https://developer.salesforce.com/page/Enforcing_CRUD_and_FLS

All Answers

Mahesh DMahesh D
Hi Sakthidasan,

Please find the below information:

Dynamic Apex enables developers to create more flexible applications by providing them with the ability to:


Access sObject and field describe information
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.

Access Salesforce app information
You can obtain describe information for standard and custom apps available in the Salesforce user interface. Each app corresponds to a collection of tabs. Describe information for an app includes the app’s label, namespace, and tabs. Describe information for a tab includes the sObject associated with the tab, tab icons and colors.

Write dynamic SOQL queries, dynamic SOSL queries and dynamic DML
Dynamic SOQL and SOSL queries provide the ability to execute SOQL or SOSL as a string at runtime, while dynamic DML provides the ability to create a record dynamically and then insert it into the database using DML. Using dynamic SOQL, SOSL, and DML, an application can be tailored precisely to the organization as well as the user's permissions.This can be useful for applications that are installed from Force.com AppExchange.

-----------------------------------------

Dynamic SOQL
Dynamic SOQL refers to the creation of a SOQL string at runtime with Apex code. Dynamic SOQL enables you to create more flexible applications. For example, you can create a search based on input from an end user, or update records with varying field names.

To create a dynamic SOQL query at runtime, use the database query method, in one of the following ways:

(1) Return a single sObject when the query returns a single record:
 
sObject s = Database.query(string_limit_1);

(2) Return a list of sObjects when the query returns more than a single record:
 
List<sObject> sobjList = Database.query(string);


Also look into the below URL:


https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic.htm

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_soql.htm

http://blogforce9.blogspot.com/2012/09/using-variables-in-dynamic-soql.html

https://eltoro.secure.force.com/DynamicApexSampleUsingSchemaDescribeDynamicSoqlAndDynamicDml

http://stackoverflow.com/questions/30709237/create-a-dynamic-soql-query-using-variable-objects

http://sfdcsrini.blogspot.com/2014/11/dynamic-apex-in-salesforce.html


Please do let me know if it helps you.

Regards,
Mahesh

SakthidasanSakthidasan
Hi Mahesh
                I can understand using dynamic apex we can get describe information of soject type ,label name, sobject relationship whether it support create or undelete operation and get field information also.and we can use dynamic soql,sosl for execute query at run time,but when should we use these dynamic concept (information)
Mahesh DMahesh D
Whenever we are not sure about the fields or where conditions then we will use Dynamic SOQLs.

Example:

 
string cntstr='';
if(ipr.ABC__Contact__c<>null){
	cntstr = 'Select Phone,OtherPhone,MobilePhone,Email,Fax from Contact where Id=\''+ipr.ABC__Contact__c+'\'';
}else if(newcs.ContactId<>null){
	cntstr = 'Select Phone,OtherPhone,MobilePhone,Email,Fax from Contact where Active__c = true AND Id=\''+newcs.ContactId+'\'';
}

if(cntstr != ''){
	Contact ct=database.query(cntstr);
}

Here if you see the query is not static, its dynamic as we are not sure about the ID which we are going to use and also the number fields in the where condition is also different.

Another situation is, whenever we are implementing a Search page where-in we have to find the records based on field values given by User then we will use the dynamix SOQL.


In these situations we will use Dynamic SOQL.

Please do let me know if it helps you.

Regards,
Mahesh
SakthidasanSakthidasan
Thanks for clarifying that,now I understand dynamic sql concept ,when should we use soject and field describe information
@Karanraj@Karanraj
Below example scenario you can able to make use of sObject and field describe (Dynamic Apex Class)

1. You want to export the all the fields from an object.
With the help of dynamic apex, you can able to retrieve all the fields and form a dynamic SOQL query and display the field in visualforce and able to export it into CSV or PDF file. In future, if the user adds any new field in the object, you don't have to modify your apex code or visualforce page to display. SObject and field describe will get the fields dynamically from the mentioned object.

2. Checking field level security 
If you want to enforce the field level security in your visualforce page [custom controller ] you have use the sObject and field describe information to check the field level security for the running user 
if (!Schema.sObjectType.Contact.fields.Name.isAccessible()){
          return '';
}
Check this article to know more details about enforcing CRUD and FLS security in visualforce page - https://developer.salesforce.com/page/Enforcing_CRUD_and_FLS
This was selected as the best answer