Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Episode 4 - Introduction to SOQL in Salesforce

468 views

Published on

Episode 4 by Amit Choudhary - Covering basic of SOQL

Published in: Education
  • Be the first to comment

  • Be the first to like this

Episode 4 - Introduction to SOQL in Salesforce

  1. 1. Path to Code Begin Your Salesforce Coding Adventure
  2. 2. Episode 4 Basics of SOQL & Best Practices
  3. 3. Salesforce MVP, Founder of ApexHours Follow me at @Amit_SFDC @ApexHours Amit Chaudhary
  4. 4. Agenda • What is SOQL • Basic of SOQL • Condition Expression Syntax (WHERE Clause) • Operators , Date Literals • Aggregate Function • GROUP BY • HAVING • Order By • LIMIT • Workbench • Relationship Queries • SOQL in Apex , Querying Record in Batches By Using SOQL For Loops • Basic of SOSL ,Best Practices
  5. 5. Some House Rules • Mute your mic • Keep adding questions in Zoom Q&A Window • No question is too small • Questions will be answered in last 15 mins
  6. 6. What is SOQL SOQL means Salesforce Object Query Language which is used to query the records from the database based on the requirement
  7. 7. Basic Of SOQL SELECT fieldList [subquery] FROM objectType WHERE conditionExpression [GROUP BY ] [HAVING havingConditionExpression] [ORDER BY ASC|DESC] [LIMIT numberOfRowsToReturn]
  8. 8. Condition Expression Syntax (WHERE Clause) • SELECT identifies what columns • FROM identifies which table • Restrict the rows returned by using the WHERE clause SELECT |{[DISTINCT] column|expression [alias],...} FROM ObjectName WHERE Condition;
  9. 9. Operators Comparison Operators (=,!=,<,<=,>,>=,LIKE,IN,NOT IN, INCLUDES , EXCLUDES) Logical Operators (AND , OR, NOT) •Comparison Operators •(=,!=,<,<=,>,>=,LIKE,IN,NOT IN, INCLUDES , EXCLUDES) •Logical Operators •(AND , OR, NOT)
  10. 10. In Condition • Use the IN membership condition to test for values in a list. SELECT BillingCity FROM Account where BillingCity in ('Singapore','Paris','Portland')
  11. 11. Using the LIKE Condition • Use the LIKE condition to perform wildcard searches of valid search string values. • Search conditions can contain either literal characters or numbers: • % denotes zero or many characters. • _ denotes one character. SELECT BillingCity FROM Account where name like ‘%IBM%’
  12. 12. Date Literals
  13. 13. ORDER BY Clause • Sort rows with the ORDER BY clause • ASC: ascending order, default • DESC: descending order • The ORDER BY clause comes last in the SELECT statement. SELECT BillingCity FROM Account order by Name
  14. 14. GROUP BY • You can use the GROUP BY option in a SOQL query to avoid iterating through individual query results. That is, you specify a group of records instead of processing many individual records. SELECT leadSource FROM Lead Group By LeadSource
  15. 15. Aggregate Function Function Example AVG SELECT CampaignId , AVG(Amount) FROM Opportunity GROUP BY CampaignId Count SELECT COUNT() FROM Account WHERE Name LIKE 'a%' Count_Distinct SELECT COUNT_DISTINCT(Company) FROM Lead MAX SELECT MIN(CreatedDate), FirstName, LastName FROM Contact GROUP BY FirstName, LastName MIN SELECT Name, MAX(BudgetedCost) FROM Campaign GROUP BY Name SUM SELECT SUM(Amount) FROM Opportunity WHERE IsClosed = false AND Probability > 60
  16. 16. Order by and Limit • Sort rows with the ORDER BY clause • ASC: ascending order, default • DESC: descending order • The ORDER BY clause comes last in the SELECT statement. SELECT name from Account Order by Name limit 10
  17. 17. Click to edit Relationship Queries
  18. 18. For child-to-parent relationships • Query child-to-parent relationships, which are often many-to-one. Specify these relationships directly in the SELECT, FROM, or WHERE clauses using the dot (.) operator. SELECT Contact.FirstName,Contact.Account.Name from Contact
  19. 19. For parent-to-child relationships • Specify these relationships using a subquery (enclosed in parentheses), where the initial member of the FROM clause in the subquery is related to the initial member of the outer query FROM clause • When you use a relationship name in a query, you must use the relationship names without the __c. Instead, append an __r (underscore underscore r). SELECT Name , ( SELECT Contact.FirstName,Contact.LastName FROM Contacts ) From Account
  20. 20. Click to editSOQL in Apex
  21. 21. Querying Record in Batches By Using SOQL For Loops • SOQL for loops iterate over all of the sObject records returned by a SOQL query • SOQL for loops can process records one at a time using a single sObject variable, or in batches of 200 sObjects at a time using an sObject list insert new Account[]{new Account(Name = 'yyy'), new Account(Name = 'yyy'), new Account(Name = 'yyy')}; Integer i = 0; for (Account tmp : [SELECT Id FROM Account WHERE Name = 'yyy']) { i++; } System.assert(i == 3); i = 0; Integer j; for (Account[] tmp : [SELECT Id FROM Account WHERE Name = 'yyy']) { j = tmp.size(); i++; } System.assert(j == 3); System.assert(i == 1);
  22. 22. What is SOSL To perform text-based queries across multiple sObjects, you can use SOSL (Salesforce Object Search Language)
  23. 23. SOSL • FIND Clause with Search Term • IN Clause :- search group • RETURNING :- which data to return • When you use a relationship name in a query, you must use the relationship names without the __c. Instead, append an __r (underscore underscore r). FIND ‘PathToCode’ IN ALL FIELDS RETURNING Account(Name), Contact(LastName, FirstName, Email)
  24. 24. Q & A
  25. 25. Thank You
  26. 26. Subscribe

×