Below example is going to fetch the fields from Account sObject, prepare the SOQL statement and then finally query Account sObject to fetch the data.
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.
Dynamic SOSL:
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.