Preface: this post is part of the Advanced Apex Concepts series.
I consider this quiz to be extremely difficult. Don’t get down if you get many wrong, you’re supposed to. At this point in your developer career it’s important to start thinking about abstract concepts. Make sure to give each question a sincere shot before looking at the answers – you’ll learn a lot!
Chapter 6 Questions:
1. You need to write a trigger that references formula fields on an object. These formula fields often change when a record is created/edited and saved. Should you use a before or after trigger?
2. You’re writing a trigger on Opportunities that reassigns the owner of newly created opps. Should you use a before or after trigger?
3. You need to write a trigger that automatically creates Contact Roles for every newly created Opportunity. Should you use a before or after trigger?
4. True or false. Every trigger you write should have at least one System.debug statement.
5. Is it better to have too many System.debug statements or too few?
6. True or false. You can see the output of System.debug in both Sandbox and Production orgs.
7. Which happens when you use Trigger.oldMap in an insert trigger?
a. The value returned is always null
b. You receive an error
8. You’re creating a trigger that reduces the inventory of a Stock object every time a Product is purchased. Why is it or is it not necessary to use Trigger.oldMap?
9. An email you’re writing needs to include multiple different variables depending on the associated Contact. Create a “body” variable for an email message that will say this for every contact in your trigger (things inside <<this>> are fields):
Dear <<Contact’s First Name>>,
Every day I get lost in your <<Contact’s eye color>> eyes. Your <<Contact’s hair color>> hair smells like <<Roses, if the “Likes Roses” field is checked. Otherwise, Dandelions>>. I want to buy you <<Contact’s favorite ice cream flavor>> ice cream that is organic. I will pick you up after you’re off work at <<Contact’s Account name>>. I will not take no for an answer.
Love,
<<Your first name>>
10. Your manager would like to edit some of your trigger’s variables without having to write new code. You have the option of saving these variables either in a Custom Setting or in a custom object. What are the advantages and disadvantages of each option?
Chapter 6 Practice Trigger:
Write a trigger that:
– Sends a “Farewell” email to Leads
– Only sends this email when the Lead’s “Rating” changes to “Cold”
– Can be turned on and off without writing any code
– Has at least five System.debug statements
– Don’t forget the test class!
Hi David,
Can you please share the links to answer sheets for Chapter-5,6 quiz?
Hi David,
I tried to solve the Chapter 6th trigger in a little more easier way.
Please give your inputs on this:
trigger Farewell on Lead (before insert, before update) {
Lead_Settings__c settings = Lead_Settings__c.getInstance(‘Bye’);
Boolean TriggerFire = settings.Trigger_Switch__c;
List mails = new List();
if(TriggerFire == True)
{
for(Lead l : Trigger.new)
{
if(l.Email != Null && Trigger.oldMap.get(l.Id).Rating != ‘Cold’)
{
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
List sendTo = new List();
sendTo.add(l.Email);
mail.setToAddresses(sendTo);
mail.setReplyTo(‘chaitanya.desai@cognizant.com’);
mail.setSenderDisplayName(‘Cognizant Technology Solutions India’);
mail.setSubject(‘FAREWELL EMAIL’);
String body = ‘Dear ‘ + l.FirstName + ‘, ‘;
body += ‘I confess this will come as a surprise to you.’;
body += ‘I am John Alliston CEO of the Cognizant.’;
body += ‘I write to bid you a great farewell’;
body += ‘We are organizing a great party for you ‘;
body += ‘Please mark your presence along with your family ‘;
mail.setHtmlBody(body);
mails.add(mail);
}
}
}
Messaging.sendEmail(mails);
}