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 3

January 30, 2014

Preface – This post is part of the Core Apex Tools series.
View Chapter 3 quiz questions without the answers!

Chapter 3 Questions:

1. What is ‘2’ + ‘2’?

The answer is ’22’. Note that each of the numbers are wrapped in single quotes, which means we treat them as text. Also note that the result is a string too!

2. What kind of data type is this value? true

Boolean.

3. Which of the following are sObjects?
– Account
– Task
– Opportunity Line Item
– Account Contact Role
– Contact History
– Price Book

All the above are sObjects. If you can run a report on it, it’s an sObject!

4. What’s the difference between a List and a Set? What type is Trigger.new?

The order of values in a List is always preserved, while in Sets the order can fluctuate. Also, Lists can have two entries that are exactly the same. Trigger.new is a List – it’ll always be in the same order every time you loop through it!

5. You have a list of “yummyDonuts” with values “chocolate glaze”, “cream filled”, and “jelly”, in that order. What does yummyDonuts[1] return? How about yummyDonuts[3]?

yummyDonuts[1] is ‘cream filled’ and yummyDonuts[3] will give you an error! Remember that the index starts at 0.

6. What is wrong with this dot notation: myContact.Best_Friend__c.Account.Opportunities.size()

__c changes to __r in dot notation! The correct statement is myContact.Best_Friend__r.Account.Opportunities.size()

7. What are two methods you can use on any List using dot notation?

Google Salesforce List Methods

8. What kind of variable would you need to store the output of this dot notation? myOpportunity.Account.Tasks

List<Task>

9. How many times would this loop run?
List leads = [SELECT Id FROM Lead LIMIT 99];
for (Integer i = 1; i < leads.size(); i++) { // Drink a beer }

98 times! Bonus points if you caught that it could be less than 98 if there were less than 99 leads in the org.

10. When should you use == as a comparison operator and when should you use .equals()?

Use .equals() for strings, and == for everything else.

11. Select true or false for each of these scenarios. The opportunity is in stage ‘Closed Won’:
!opp.StageName.equals(‘Negotiation’) || !opp.StageName.equals(‘Closed Won’)

!(opp.StageName.equals(‘Open’) && opp.StageName.equals(‘Pending’))

!opp.StageName.equals(‘Demo’) && (!opp.StageName.equals(‘Open’) && !opp.StageName.equals(‘Closed Lost’))

opp.StageName.equals(‘Pending’) || (!opp.StageName.equals(‘Demo’) && opp.StageName.equals(‘Closed Won’))

True, True, True, True

12. What will my status be if I buy two dozen Krispy Kreme donuts?
String status;
if (donuts.size() > 20) {
  status = ‘Heartburn’;
} else if (donuts.size() > 10) {
  status = ‘Eazy Peazy’;
} else if (donuts.size() == 24) {
  status = ‘King of Donut Land!’;
}

‘Heartburn’ – remember that only the first match is returned!

13. What’s wrong with this DML code?
Contact me = new Contact();
me.FirstName = ‘David’;
me.LastName = ‘Liu’;
update me;

me needs to be inserted since it’s a new record!

14. Does this DML statement work?
List<Account> accounts = [SELECT Id FROM Account];
delete accounts;

Yes – you can do DML on lists.
36 Comments
Bheem
November 13, 2017 @ 5:34 am

Great work Devid,

thanks a lot..!)

Reply
Bhavana Singh
June 8, 2017 @ 2:40 pm

Isn’t there a typo here

List leads = [SELECT Id FROM Lead LIMIT 99];
for (Integer i = 1; i < leads.size(); i++) { // Drink a beer }

Shouldn't first line be List leads = [SELECT Id FROM Lead LIMIT 99];

(and change ‘Drink a beer’ to ‘take a sip of your beer’. We don’t want anyone drinking 98 beers :)

Love your site

Reply
Stijn Blommerde
December 17, 2015 @ 2:25 am

i have some difficulty understanding the standard salesforce objects, their fields and relations. i don’t have a marketing background, so a Line Item, Role, Price Book or History don’t mean much to me and i cannot clearly distinguish objects from fields. is there a resource available to explain these standard objects , their fields and relations?

Reply
    David Liu
    December 20, 2015 @ 6:28 pm

    I actually don’t know of any good tutorials explaining these. Hopefully someone else can point you to some!

    Reply
      Stijn Blommerde
      December 21, 2015 @ 2:06 am

      sorry for my odd question. i did not start my career as an admin. i am a self-taught programmer, recently hired as a python developer. My company works with Salesforce and asked me to broaden my skill-set.

      i have found a tutorial on CRM basics at trailhead that explains the standard objects.
      https://developer.salesforce.com/trailhead/module/admin_intro_crm_basics

      thanks a lot for the website. it is well explained and really helpful.

      i would opt for extra challenges/exercises for SOQL and Apex. in the style of codingbat.com.

      Reply
Shobhit Tuteja
August 21, 2014 @ 11:13 pm

Hi David,

For Question No. 11 Last part I think the answer should be False as :

opp.StageName.equals(‘Pending’) || (!opp.StageName.equals(‘Demo’) && opp.StageName.equals(‘Closed Won’))

False OR (True AND False)

False OR False

False —–> Final Answer

Correct me if i am wrong

Thanks,

Shobhit

Reply
    David Liu
    August 21, 2014 @ 11:15 pm

    Here’s what I got!

    False OR (True AND True)

    Reply
rachel
August 18, 2014 @ 7:52 pm

Can you explain #11?

Also, how would I populate a field on a child record of contacts, with a field from the most recent opportunities they have.

Reply
    David Liu
    August 18, 2014 @ 8:12 pm

    Try using SOQL on the OpportunityContactRole object to get fields from the Contact or most recent Opportunity!

    Reply
Anonymous
August 17, 2014 @ 8:17 pm

hi, can you please explain and number 11?

Reply
    David Liu
    August 18, 2014 @ 7:52 pm

    Try to replace each statement with true/false:
    TRUE || FALSE
    TRUE && FALSE || TRUE

    etc etc

    Reply
Kruthi
June 27, 2014 @ 12:36 pm

Hi David,

Can you please explain the answer for question 6.
I do understand we use the child relationship name for downward queries however I am not sure how are we using Opportunities in the below statement?
myContact.Best_Friend__r.Account.Opportunities.size()

Reply
    David Liu
    June 28, 2014 @ 11:31 am

    So it goes like this:

    (Upwards) Contact to Best Friend Contact
    (Upwards) Best Friend Contact to Account
    (Downwards) Account to Opportunities

    An Account can have multiple Opportunities so it’s downwards – also we don’t use __r for Opportunities because it’s a standard relationship!

    David

    Reply
      radha
      February 15, 2015 @ 11:27 pm

      SELECT xyz, Account__r.Name FROM Order_Header__c.
      here we used __r for account because it is custom relation ship
      but in this “myContact.Best_Friend__r.Account.Opportunities.size()” we dont add__r for Account
      can u please explain this

      Reply
        David Liu
        February 16, 2015 @ 12:56 pm

        The first relationship is from Order Header (custom object) –> Account which is a custom relationship via a custom field

        The second relationship is from Contact –> Account and that one is a standard relationship via a standard “Account” field =)

        Reply
          radha
          February 18, 2015 @ 11:31 am

          thank u

          Reply
Sunny Kapoor
May 14, 2014 @ 6:34 am

For your ques given as homework :)
Writing a trigger that creates a new opportunity every time an account is created

Is this right?

trigger insertOpp on Account (after insert) {

for(Account acc : Trigger.New)
{
opportunity opp = new opportunity();
opp.name = ‘Test opp’;
opp.CloseDate = Date.Today();
opp.Stagename=’Prospecting’;
opp.AccountId= acc.id;
insert opp;

}

}

Reply
    David Liu
    May 15, 2014 @ 2:28 pm

    We’ll done Sunny!!! Time to move on to more difficult challenges and chapters!!

    Reply
      Sunny Kapoor
      May 15, 2014 @ 10:46 pm

      Thanks David and great blog :)

      Reply
Virginia
April 28, 2014 @ 8:22 am

Great explanation on sObjects! I’ve been trying to get my head wrapped around that concept and I think I finally got it!

Reply
Zak Crammond (@zcrammond)
April 22, 2014 @ 8:49 am

David,

the link in the answer to number 7 is broken, i ‘think’ it should direct to this page?

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_list.htm

loving this site this afternoon, as a completely off topic to this particular post, did you fancy building a page of simple triggers that we could write with an ‘answers’ section so we can compare the code we write with what you believe to be the correct process?

my biggest failing of a budding developer is trying to identify a need for a trigger!

Reply
    David Liu
    April 22, 2014 @ 9:09 pm

    Thanks for letting me know Zak, the link is fixed!!

    That’s a great idea – you guys need more homework! For now, try:
    Writing a trigger that creates a new opportunity every time an account is created

    I’ll grade you!
    David

    Reply
Teja
April 16, 2014 @ 1:37 pm

When I was looking at the answers I saw that What type is Trigger.new? question was not answered? Is this by design? Just wanted to bring it to your attention in case the answer decided to take a vacation on its own. :) Awesome work though!

Reply
    David Liu
    April 16, 2014 @ 10:25 pm

    You are totally right – the answer took a vacation!!! hahahaha

    Trigger.new is a list =) It has a particular order!
    https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_triggers_context_variables.htm

    David

    Reply
      Devanshu Sharma
      May 15, 2014 @ 12:48 pm

      14. Does this DML statement work?
      List accounts = [SELECT Id FROM Account];
      delete accounts;

      it will through an error , it should be List accounts = [SELECT Id FROM Account];

      :-)

      Reply
        David Liu
        May 17, 2014 @ 10:49 am

        Great catch!!!! Fixed!

        Reply
newsfdcdev
April 2, 2014 @ 8:31 pm

For 11) How is the second one true?

!(opp.StageName.equals(‘Open’) && opp.StageName.equals(‘Pending’))

True and False = False

Reply
    David Liu
    April 2, 2014 @ 9:19 pm

    !(False && False)
    !(False)
    True

    Keep an eye out on the parenthesis =)

    Reply
      newsfdcdev
      April 2, 2014 @ 11:45 pm

      doh! Thanks for clarifying

      Reply
      Virginia
      May 16, 2014 @ 8:03 am

      Piggy backing on newsfdcdev question on #11. The first scenario is through me for a loop….

      !opp.StageName.equals(‘Negotiation’) || !opp.StageName.equals(‘Closed Won’)

      If the Stage is ‘Closed Won’, isn’t the statement above saying:

      StageName does not equal Negotiation
      or
      StageName does not equal Closed Won

      Since the Stage is ‘Closed Won’ wouldn’t the statement be FALSE?

      As always…thank you for your dedication to help the rest of us!

      Reply
        Matt
        May 16, 2014 @ 2:28 pm

        I believe for an OR statement, just one of the tests has to be true for the statement to equate to true. So in the scenario, the first test is True (stage does not equal Negotiation) and the second is False (stage does equal Closed Won), but since one was True the statement is True. Hopefully David will verify this.

        Thanks David for this site. I stumbled across this a couple weeks ago, and finally bit the bullet to learning Apex. So far this is great information, in an easy to understand presentation.

        Reply
          David Liu
          May 17, 2014 @ 10:29 am

          Thank you Matt and you are 100% correct about the OR statement!

          Reply
John
January 30, 2014 @ 11:41 pm

Awesome site. Feel like I finally found a good explanation of Lists vs. Maps vs. Sets after scouring the interweb.

Appreciate the great explanations and plain speak for those of us that are new to the coding game.

Keep up the good work!

Reply
    David Liu
    January 31, 2014 @ 8:05 pm

    Coding isn’t as difficult as most make it seem – you can do it!!!

    Reply
      Harish
      March 22, 2014 @ 12:39 am

      I always feel difficult to learn coding but after going through sfdc99.com i feel it is easy if we concentrate on it.

      Reply
        David Liu
        March 23, 2014 @ 12:45 am

        Harish, you are my brother. I wish you the best in your future development career =)

        Reply

Leave a Reply Cancel reply

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


*

*

Theme: Simple Style by Fimply