What is dynamic SOQL?

+2 votes
asked Nov 27, 2015 in Developer by bukolasf (400 points)

1 Answer

+1 vote
answered Dec 4, 2015 by darsh (3,020 points)

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:

Return a single sObject when the query returns a single record:

sObject s = Database.query(string_limit_1);

Return a list of sObjects when the query returns more than a single record:

List<sObject> sobjList = Database.query(string);

The database query method can be used wherever an inline SOQL query can be used, such as in regular assignment statements and for loops. The results are processed in much the same way as static SOQL queries are processed.

Welcome to I Can Crack, where you can ask questions and receive answers from other members of the community.

68 questions

47 answers

3 comments

5,318 users

...