in Technology by

Explain the types of SOQL statements in Salesforce.

1 Answer

0 votes
by

Salesforce Object Query Language(SOQL) is useful in querying the records from database.com depending on the requirement. There are 2 types of SOQL statements: Static SOQL and Dynamic SOQL.

Static SOQL:

  • The Static SOQL statement is written using [] (array brackets). This statement is similar to IINQ (Ion Integrated Query).
  • It is used in the condition where you don’t have any dynamic changes in the SOQL query. We don’t use Static SOQL when the field name or where the condition is needed to be dynamically defined.

Example for Static SOQL query:

Contact[] contacts = [SELECT testfield__C, FirstName, LastName FROM Contact WHERE LastName = 'Dhanya'];

Dynamic SOQL:

  • It is used to refer to the SOQL string creation at run time with Apex code.
  • Dynamic SOQL allows you to create more flexible applications. For example, you are able to create a search based on end-user input or update records with different field names.
  • To create a Dynamic SOQL query at run time using a Database.query() method, in any one of the given below ways:
    • Return a single sObject when the single record is returned by a query.
      sObjects s = Database.query(string_limit_1);
    • Return a list of sObjects when more than a single record is returned by a query.
      List<sObject> sobjList = Database.query(string);

Example for Dynamic SOQL query:

String myTestString = 'TestName';
List<sObject> sobjList = Database.query('SELECT Id FROM MyCustomObject__c WHERE Name = :myTestString');

Related questions

0 votes
    What is the difference between SOQL and SOSL in Salesforce?...
asked Nov 11, 2020 in Technology by JackTerrance
0 votes
    can anybody help me with writing a SOQL query. Using "Date" data type in WHERE statement is easy, ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    How do I make a SOQL query like this? SELECT id FROM Account WHERE LastActivityDate = 30_DAYS_AGO This produces an ... = 30_DAYS_AGO ^ Select the correct answer from above options...
asked Feb 2, 2022 in Education by JackTerrance
0 votes
    What are the general description for loop statements and available loop types in C?...
asked Nov 8, 2020 in Technology by JackTerrance
0 votes
    What are the different types of email templates available in Salesforce?...
asked Jun 26, 2021 in Technology by JackTerrance
0 votes
    What are the different types of apps we can use in Salesforce?...
asked Jun 25, 2021 in Technology by JackTerrance
0 votes
    What are the types of reports that we have in Salesforce?...
asked Nov 12, 2020 in Technology by JackTerrance
0 votes
    What all data types can a set store in Salesforce?...
asked Nov 11, 2020 in Technology by JackTerrance
0 votes
    What is an external ID in Salesforce? Which all field data types can be used as external IDs?...
asked Nov 11, 2020 in Technology by JackTerrance
0 votes
    What are the types of custom settings in Salesforce? What is the advantage of using custom settings?...
asked Nov 11, 2020 in Technology by JackTerrance
0 votes
    What are the different types of object relations in salesforce? How can you create them?...
asked Nov 11, 2020 in Technology by JackTerrance
0 votes
    What are the different types of reports available in Salesforce? Can we mass delete reports in Salesforce?...
asked Nov 11, 2020 in Technology by JackTerrance
0 votes
    What are the different types of email templates that can be created in Salesforce?...
asked Nov 11, 2020 in Technology by JackTerrance
0 votes
    What is a sandbox org? What are the different types of sandboxes in Salesforce?...
asked Nov 11, 2020 in Technology by JackTerrance
0 votes
    What are the different types of reports available in Salesforce?...
asked Jun 25, 2021 by JackTerrance
...