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!
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
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
This post is your friend =)
https://www.sfdc99.com/2014/02/22/debug-your-code-with-system-debug/
Hi David, thanks for all the efforts that you put in. Newbie’s have a great platform in SFDC99 to learn and explore APEX.
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!
Well done Zak this is amazing!!!!!