Difference between Static SOQl and Dynamic SOQl
Salesforce Object Query Language (SOQL) is used to search records in your Salesforce organisation. SOQl queries are two types they are Static SOQL and Dynamic SOQl.
Static SOQl statements.
Static SOQl query is written in [ ] (array brackets). It is good to use when you didn’t have any dynamic changes in the soql query. when the fields names or where conditions is needed to be defined dynamically we didn’t use static SOQL.
Contact contacts = [SELECT testfield__C, FirstName, LastName FROM Contact WHERE LastName = 'Prasanth'];
Dynamic SOQL refers to the creation of a SOQl string at run time 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. This is the major difference between soql and dynamic soql.
- Dynamic SOQL enables to create more flexible applications.
- To create Dynamic SOQL query at run time use Database.query(); method.
To create Dynamic SOQL query at run time, use the database query method as shown below.
List<sObject> sObject = Database.query(string);