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 – Chapter 4

February 1, 2014

Preface – This post is part of the Write Your First Intermediate Trigger series.

Chapter 4 Questions:

1. Without SOQL, what fields are available for each record entering a trigger?

2. What character must be placed before every Apex variable used in a SOQL query?

3. What does an error look like to the end user when adding an error to a record using the .addError() method?

4. Why shouldn’t a developer query for production records in a test class?

5. Why should every test class use System.assertEquals(), even if your code works 100% of the time?

6. What’s the negative test case for a trigger that divides the Amount by the number of days until the Close Date?

7. Which is the correct way of using System.assertEquals()?
System.assertEquals(‘English Bulldog’, favoriteDog);
System.assertEquals(favoriteDog, ‘Toy Poodle’);

Chapter 4 Practice Trigger
Write a trigger on Leads that checks to see if another Lead or Contact has the exact same name. If so, populate a “Potential Lead Duplicate” or a “Potential Contact Duplicate” field. Don’t forget the test class!

Answers

6 Comments
radha
October 8, 2015 @ 9:59 pm

after developing this practice trigger and the deduping trigger in the example ,when I tried ti convert lead using convert button I am getting this error

Error: System.DmlException: Update failed. First exception on row 0 with id 00Qi000000EtbSaEAJ; first error: CIRCULAR_DEPENDENCY, attempt to violate hierarchy constraints: [Lead_potential_duplicate__c] (System Code)

can u tell me why
Thanks in advance

Reply
Anonymous
December 18, 2014 @ 10:38 pm

Hi David,

I wrote the trigger. No errors were reported but the fields are not populated as is required………

Not able to figure out….do please help. This is my first trigger.

trigger trg on Lead (before insert,before update) {
list la= new list();
list ca=new list();
for(lead lb : trigger.new)
{
for(lead l3: la)
{
if(l3.Title==lb.Title)
l3.dupe__c= ‘Duplicate is created’;

}

for(contact c3: ca)
{
if(c3.Title==lb.Title)
c3.dupe__c= ‘Duplicate is created’;
}

}

}

I strongly believe that I do not have to call update dml separately but calling it also did not yield any results

Lourd

Reply
    David Liu
    December 18, 2014 @ 10:54 pm

    This post is your friend =)
    https://www.sfdc99.com/2014/02/22/debug-your-code-with-system-debug/

    Reply
Satish
June 11, 2014 @ 2:25 am

Hi David, thanks for all the efforts that you put in. Newbie’s have a great platform in SFDC99 to learn and explore APEX.

Reply
Zak
May 23, 2014 @ 2:49 am

my practice trigger:

trigger CreateDupeLead on Lead (Before insert, Before update) {

list LeadEmails = new list ();
Map leadsByEmail = new Map();

for (Lead myLead : trigger.new) {
if (myLead.Email !=null) {
LeadEmails.add(mylead.email);
leadsByEmail.put(myLead.Email, myLead);
}
}
list dupes = [select id from contact Where Email = :LeadEmails];
if (Dupes.size() >0) {
for(Contact c : dupes){
Lead dupeLead = leadsByEmail.get(c.Email);
if(dupeLead.FirstName == c.FirstName || dupeLead.LastName == c.LastName){
dupeLead.PotentialDuplicate__c = true;

}
}
}
}

Next step is probably to cut that into a class so the trigger only calls the method, and then the dreaded test class……

btw, this took me HOURS to get to bulkify and compile!

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

    Well done Zak this is amazing!!!!!

    Reply

Leave a Reply Cancel reply

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


*

*

Theme: Simple Style by Fimply