Founder of Technical Potpourri, Co-Founder of Shrey Tech, Enterprise Cloud Architect
In this post, I will explain another very powerful feature of Dynamic Apex i.e. Dynamic SOQL and SOSL.
In terms of governor limits, Dynamic SOQL queries have the same governor limits as static queries.
Dynamic SOQL Consideration
In Dynamic SOQL, you cannot use bind variable strings in a query string. For example, the below section of code will not work and will throw an error that Variable doesn’t exist.
To solve that you need to resolve the variable into a String variable and then use in Dynamic SOQL. Below is the code which will work.
With Dynamic SOSL, developer can create SOSL statement in runtime thus allowing to build more flexible applications.
You can execute the below statement for dynamic SOSL:
Dynamic SOSL statement returns a list of lists of sObjects, where each list contains the search result for a particular sObject type. Results will be stored in the same order as the sObjects are specified in the dynamic SOSL query. Dynamic SOSL can’t be used where an inline SOSL can be used, such as in regular assignments and for loops.
Dynamic SOSL is also having the same governor limits as static SOSL statements.
Below code will search for the keyword in the object – Account, Lead, Contact and sudipta__Book__c.
Please provide your feedback. Thanks.
This article is not endorsed by Salesforce, Google, or any other company in any way. I shared my knowledge on this topic in this blog post. Please always refer to Official Documentation for the latest information.