1

I am having an issue with my trigger. So I created a trigger that sends emails to the Sales rep when a duplicate domain is detected in the leads. The domain is checked from the lead object to the contact object. My trigger works fine, just for one problem. So the problem is that if another field is updated within the Lead, the trigger sends another email. I do not want the trigger to send emails if other fields are updated only if the email is updated. Is there any way to go about solving this?

trigger FindDupes on Lead (before insert, before update) {
    List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>(); //master list that will hold all the emails that we send out
    for (Lead myLead : Trigger.new){

        if(myLead.Domain__c != null){
            List<Contact> dupes = [SELECT Domain__c  FROM Contact
                                       WHERE Domain__c = :myLead.Domain__c];

            if(dupes.size() > 0 && myLead.IsConverted==False ){


                    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();


                    List<String> sendTo = new List<String>();

                    sendTo.add('[email protected]');

                    mail.setToAddresses(sendTo);

                    //mail.setReplyTo('[email protected]');
                    mail.setSenderDisplayName('Salesforce Administrator');



                    mail.setSubject('IMPORTANT MESSAGE');
                    String body = 'Hello' + ', ';
                    body+= 'the domain of the lead that just came in is already in our system.' + '**********';
                    body+= ' '+ ' Lead Name: ' + myLead.FirstName + ' ' + myLead.LastName + '**********';
                    body+= ' '+ ' Company Name: ' + myLead.Company;


                    mail.setHtmlBody(body);

                    mails.add(mail);

                    Messaging.sendEmail(mails);
                }else{
                    myLead.Email = myLead.Email;
                }

        }

     }
}
6
  • 2
    Please edit your post to include the code you have so far.
    – Adrian Larson
    Commented Sep 16, 2016 at 14:52
  • yes of course, hold on
    – Alias311
    Commented Sep 16, 2016 at 14:53
  • I have edited the post with the code I have
    – Alias311
    Commented Sep 16, 2016 at 14:55
  • 1
    you are doing soql inside of a for loop -- this trigger needs reworking to be bulkified as otherwise you'll blow up governor limits
    – cropredy
    Commented Sep 16, 2016 at 14:59
  • 1
    @Alias311 Exactly. You're currently making 1 query per lead, so if you ever inserted or updated 101 or more leads at once, you would exceed the SOQL query limit and get an exception.
    – Derek F
    Commented Sep 16, 2016 at 15:14

1 Answer 1

4

You can filter records whose Email has changed by looking at trigger.new and comparing it with trigger.oldMap. I would suggest moving your code out of the trigger body. Take a look at Trigger Frameworks and Apex Trigger Best Practices .

public static List<Lead> hasEmailChanged(List<Lead> newRecords, Map<Id, Lead> oldMap)
{
    List<Lead> results = new List<Lead>();
    for (Lead record : newRecords)
        if (record.Email != oldMap.get(record.Id).Email)
            results.add(record);
    return results;
}
public static void sendEmails(List<Lead> filterResults)
{
    // do stuff
}

As mentioned in the comments, you have some other issues to address, such as bulkifying your query to remove it from the loop. But the above should show you how to apply the filter you need.

2
  • Filtering changed emails is part of the solution, but there would need to be some additional logic (or a separate method) to properly handle the Before Insert event.
    – Derek F
    Commented Sep 16, 2016 at 15:08
  • Or just pass trigger.new unfiltered in that case.
    – Adrian Larson
    Commented Sep 16, 2016 at 15:37

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .