Deprecated: Optional parameter $list declared before required parameter $is_script is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 21

Deprecated: Optional parameter $register declared before required parameter $footer_or_media is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 45

Deprecated: Optional parameter $register declared before required parameter $footer_or_media is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php on line 104

Deprecated: Optional parameter $expire declared before required parameter $path is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_functions.php on line 54

Deprecated: Optional parameter $depth declared before required parameter $output is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/themes/entaro/inc/classes/megamenu.php on line 155

Deprecated: Optional parameter $depth declared before required parameter $output is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/themes/entaro/inc/classes/mobilemenu.php on line 147

Deprecated: Optional parameter $args declared before required parameter $wp_customize is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/extensions/customizer/extension_customizer.php on line 583

Deprecated: Optional parameter $args declared before required parameter $wp_customize is implicitly treated as a required parameter in /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/extensions/customizer/extension_customizer.php on line 606

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831

Warning: Cannot modify header information - headers already sent by (output started at /home1/oijoiv2f/public_html/wp-content/plugins/apus-framework/libs/redux/ReduxCore/inc/class.redux_cdn.php:21) in /home1/oijoiv2f/public_html/wp-includes/rest-api/class-wp-rest-server.php on line 1831
{"id":854,"date":"2017-07-27T06:07:39","date_gmt":"2017-07-27T06:07:39","guid":{"rendered":"http:\/\/www.salesforcenextgen.com\/?p=854"},"modified":"2020-12-28T19:45:21","modified_gmt":"2020-12-28T19:45:21","slug":"soql-salesforce-object-query-language","status":"publish","type":"post","link":"https:\/\/salesforcenextgen.com\/soql-salesforce-object-query-language\/","title":{"rendered":"SOQL (Salesforce Object Query Language)"},"content":{"rendered":"

SOQL (Salesforce Object Query Language)<\/strong><\/p>\n

SOQL is the Salesforce Object query language, it is similar to SQL in syntax. SOQL is a tool which helps us to retrieve a record from database in Salesforce. A sample SOQL is written below:<\/p>\n

<\/p>\n

Select Id, Name from Account;<\/p>\n

The result of the above statement is a list of accounts, i.e. a SOQL returns a list of sObject. We can rewrite the above statement as below.<\/p>\n

List<account> examplelist = [Select Id, Name from Account];<\/p>\n

Individual records can be accessed from this list using a loop. We can use SOQL to create a new sObject as well.<\/p>\n

Contact newContactObject = new contact(Account = [select name from account where billingCity = \u2018new delhi\u2019 limit1 ];)<\/p>\n

newContactObject.firstname = \u2018test\u2019;<\/p>\n

newContactObject.lastname = \u2018contact\u2019;<\/p>\n

Working with the result of SOQL Query<\/strong><\/p>\n

The result of the query only include the fields which are mentioned in the query, if we try to access a field which was not fetched in the query then we end up having a runtime error.<\/p>\n

Account a = [Select id from account limit 1].name\u00a0\u00a0\u00a0 —- this will throw runtime error<\/p>\n

Account a = [Select id,name from account limit 1].name\u00a0\u00a0\u00a0 —- this will run uninterrupted<\/p>\n

Accessing fields of sObject through relationship in SOQL<\/strong><\/p>\n

\u00a0<\/strong><\/p>\n

There are two types of relationship when it comes to fetching record from SOQL<\/p>\n

    \n
  1. Fetching values of parent record from a child record<\/li>\n
  2. Fetching all the child records of a parent.<\/li>\n<\/ol>\n

    The first scenario is achieved using a dot notation in SOQL query as written below:<\/p>\n

    Select id, name, parentobj.name, parentobj.custfield__c from myObject__c limit 1;<\/p>\n

    The second scenario is achieved by using a nested query as mentioned below:<\/p>\n

    Select id, name, (select firstname, lastname from contacts) from account;<\/p>\n

    When we write a nest query we make use of the child relationship name as \u2018contacts\u2019 is the relationship name between account and contact, we have used the plural of contacts. It is a user defined text for a custom relationship.<\/p>\n

    In the custom relation we make use of \u2018__r\u2019 instead of the regular \u2018__c\u2019 keyword, as written below.<\/p>\n

    Select Id, name, custObject__r.name\u00a0 from contact limit 1;<\/p>\n

    *Note, it is advised that we should avoid searching for records which contains a null value, this helps in improving the performance of the query.<\/p>\n

    How to use Apex variable in SOQL Queries<\/strong><\/p>\n

    SOQL can reference apex variable in the statement, variable has to be prefixed with a colon(:) as written below:<\/p>\n

    Contact con = new contact(lastname = \u201ctest\u201d, firstname =\u201dcontact\u201d);<\/p>\n

    Insert con<\/p>\n

    Select name from contact where Id =:con.Id;<\/p>\n

    We can use apex variable in the where clause, In or Not In clause, numeric values of limit or offset.<\/p>\n

    SOQL can also fetch the records which have been soft deleted from the org i.e. they are still present in recycle bin. We just have to add literal ALL ROWS at the end of the query to fetch all the records in an org, as mentioned below.<\/p>\n

    Select name from contact ALL ROWS;<\/p>\n

    How to write SOQL for a for Loop<\/strong><\/p>\n

    Records returned by the SOQL can be used as an input for for loop the syntax is written below.<\/p>\n

    For( myVar : [Query]){<\/p>\n

    }<\/p>\n

    Variable should be of same sObject type as of the SOQL query.<\/p>\n

    Regular SOQL and SOQL in the for loop differ from each other on the basis of the method they use to fetch record from the database, SOQL for loop use efficient chuning of data and make calls to query and querymore method where as standard SOQL can only return number of records or number of sObjects.<\/p>\n

    We should make sure that we are not making any DML call inside for loop as it results in breach off governor limit. Therefore we should use a list of sObject and add the individual objects to this list and then after the execution of the loop we should perform a DML Operation.<\/p>\n

    Next:\u00a0Playing with sObject Collections<\/a><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"

    SOQL (Salesforce Object Query Language) SOQL is the Salesforce Object query language, it is similar to SQL in syntax. SOQL is a tool which helps us to retrieve a record from database in Salesforce. A sample SOQL is written below:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[72],"tags":[40,61,74,45,75,38,42,55,51,52],"_links":{"self":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts\/854"}],"collection":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/comments?post=854"}],"version-history":[{"count":3,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts\/854\/revisions"}],"predecessor-version":[{"id":2213,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/posts\/854\/revisions\/2213"}],"wp:attachment":[{"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/media?parent=854"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/categories?post=854"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salesforcenextgen.com\/wp-json\/wp\/v2\/tags?post=854"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}