Salesforce coding lessons for the 99%
Finally, Apex tutorials for point-and-click admins! Written by a self-taught Google engineer.
  • Beginner Tutorials
    • Apex
    • Certifications
    • Career Info
    • Technical Architect
    • Visualforce
    • Videos
  • Apex Academy
  • Success Stories
  • About Me
  • Misc
    • Mailbag
    • Challenges
    • Links
    • Login to my Org
Follow @dvdkliuor SUBSCRIBE!

Quiz Answers – Chapter 2

January 29, 2014

Preface: this post is part of the SOQL: A Beginner’s Guide series.
View Chapter 2 quiz questions without the answers!

Chapter 2 Questions:

1. Why do 95% of triggers use SOQL?

SOQL lets you do cross-object actions in a trigger. If you don’t need SOQL in your trigger, there’s a strong chance you can use a workflow instead.

2. What are the two words that appear in every SOQL statement?

SELECT and FROM

3. Name at least three places you can write SOQL.

Data Loader, Workbench, Trigger

4. Which of these two statements will break and why?
SELECT Id FROM User WHERE IsActive = true
SELECT Id FROM User WHERE IsActive = ‘false’

The second version will break because it uses single quotes, which are used for text, ID, or picklist fields. Checkbox fields don’t use quotes!

5. Finish this statement which wants to query all Leads with an email address:
SELECT Id FROM Lead WHERE Email

!= null

6. Finish this statement, which wants to query all Accounts with names that start with an ‘A’
SELECT Id FROM Account WHERE Name LIKE

‘A%’

7. Write a query that gets all Opportunities with a Close Date in the future.

SELECT Id FROM Opportunity WHERE CloseDate > TODAY

8. Which direction are these relationships? Upwards or downwards.
a. [Upwards] Contacts to Accounts
b. [Downwards] Accounts to Tasks
c. [Upwards] Opportunity Line Items to Opportunities
d. [Downwards] Accounts to Quote Line Items
e. [Upwards] Campaign Members to Accounts
f. [Upwards] Opportunity History to Accounts

9. When do you use __r instead of __c?

When you are traversing a custom relationship (upwards or downwards). Use __c if you only need the ID of the related record, ie Agent__r.Id and Agent__c are the same, although the second version is preferred.

10. Write a query that gets a Contact’s Account owner’s username and their best friend as well as all related Tasks that were created in the last 90 days or have both a comment that contains the words ‘nutella’ and ‘krispy kreme’ and the Task creator’s title is populated. Oh yes – all the contacts have to weigh more than 120 pounds and have a best friend whose owner’s name ends in a ‘d’. No more than 100 records please… whew!

SELECT Id, Account.Owner.Username, Best_Friend__c, 
  (SELECT Id FROM Tasks
    WHERE CreatedDate = LAST_90_DAYS 
      OR (Description LIKE '%nutella%' 
         AND Description LIKE '%kripsy kreme%'
         AND CreatedBy.Title != null)
  ) 
  FROM Contact 
  WHERE Weight__c > 120
    AND Best_Friend__r.Owner.LastName LIKE '%d'
  LIMIT 100
15 Comments
David Cheyne
December 23, 2019 @ 1:48 pm

‘%kripsy kreme%’ ? Shouldn’t it be ‘%krispy kreme%’. ;-)

Reply
Sami
August 26, 2019 @ 6:31 pm

I didn’t get everything exactly right on #10, but I came pretty close! I misunderstood where to pull Best Friend from the first time (I grabbed the Contact’s Account Owner’s best friend). I forgot my parentheses around the three criteria in the OR statement. And I forgot to add the LIMIT to 100 records.

I’m happy with that for my very first attempt at a complicated SOQL search ever!

Reply
Lukáš
March 12, 2018 @ 7:49 am

Hi David,

I finished your SOQL session and after every part of tutorial I fell stronger and stronger in my knowledge!:) I hope it will end with DEV cert=) Thank you!

Reply
    David Liu
    March 12, 2018 @ 9:20 pm

    Woohoo!! Good luck on the cert!

    Reply
iCanCode
October 21, 2014 @ 3:17 pm

When I run the first part of my query: SELECT Id, Account.Owner.Username, regalray1__Best_Friend__r.Name,(SELECT Id FROM Tasks WHERE CreatedDate= Last_90_Days OR (Description LIKE ‘%nutella%’ AND Description LIKE ‘%kripsy kreme%’)) FROM Contact
I am not hitting any errors in the query editor. But seeing the following error when running the query:

[object Object]: CreatedDate= Last_90_Days OR (Description LIKE ‘%nutella%’ AND ^ ERROR at Row:1:Column:126 field ‘Description’ can not be filtered in query call

Can you please help?

Reply
    David Liu
    October 21, 2014 @ 9:07 pm

    ooo the key is in the final part of the error message:
    “‘Description’ can not be filtered in query call”

    Basically it looks like you can’t have the Description field anywhere in your WHERE clause! The field is too big I believe (text area long) to filter on it.

    Reply
      Anonymous
      April 8, 2015 @ 4:30 am

      Could anyone please tell me how to solve the Description filter problem that has been raised but not given solution how to resolve it. thank you in advance

      Reply
Rachel
August 12, 2014 @ 10:19 am

For #5, can I also do
5. Finish this statement which wants to query all Leads with an email address:
SELECT Id FROM Lead WHERE Email LIKE ‘%@%’

Reply
    David Liu
    August 12, 2014 @ 12:34 pm

    Yup, that definitely also works too!

    Reply
Kruthi
June 25, 2014 @ 3:42 pm

I got it too… :) Thanks David, all because of your support.

Reply
    David Liu
    June 26, 2014 @ 9:00 pm

    Good job Kruthi!!

    Reply
Nitin
June 5, 2014 @ 7:48 am

Whoooohooooo… I got the query ALMOST correct in my first attempt (the format and the brackets and all) :D
was just unaware of the LAST_90_DAYS keyword and couple of field name which I am looking for the first time. You are too good David …. GOD BLESS YOU …!!!!

Reply
    David Liu
    June 8, 2014 @ 8:25 pm

    Go Nitin!!! woohooooo hahaha

    Reply
Swathi
May 23, 2014 @ 11:26 am

Hi David ,

I have a lot of fun answering the questions! Would love it if you could add more questions! :)

Reply
    David Liu
    May 24, 2014 @ 12:33 am

    Thanks!

    Check out the webinar recordings, lots of great questions there! (won’t be up for a few more days)!

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *


*

*

Theme: Simple Style by Fimply