Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

in my requirement if any duplicate lead is created send email to lead owner code executed successfully but emails are not sending , what is the problem ?
my trigger

trigger LeadCategory on Lead (before insert) {
    LeadVerification.duplicate(trigger.new);
}

and my class is

public class LeadVerification {   
public Static void duplicate(List<Lead> leads){
   List<messaging.Email> emails=new List<messaging.Email>();
    List<Lead> upd=new List<Lead>();
    List<Lead> totalLeads=[select Company,Email,Phone,Industry,OwnerId,Owner.Name,owner.Email,LastModifiedDate from Lead];
    for(Lead l1:leads){
        for(Lead l2:totalLeads){
            if(l1.Company==l2.Company && l1.Email==l2.Email && l1.Phone==l2.Phone && l1.Industry==l2.Industry){
                messaging.SingleEmailMessage email=new messaging.SingleEmailMessage();
                string[] em= new string[]{l2.owner.Email};
                email.setToAddresses(em);
                email.setSubject('this is test');
                email.setPlainTextBody('this is verification of mail sending');
                emails.add(email);             
                l1.addError('duplicates record');
            }
        }
    }
    messaging.sendEmail(emails);
    }
}

thanks is advance

share|improve this question
up vote 5 down vote accepted

It's not sending because you're using the addError method for every email you add to your email list. Remove that from your trigger and the emails will be sent.

Edit

You don't say if this is a before insert trigger or after insert, but I'm going to assume it handles the insert. Here's one way of handling things. You could also send list dupes to an external email handler class after inserting toInsert.

public class LeadVerification {   
public Static void duplicate(List<Lead> leads){
   List<messaging.Email> emails=new List<messaging.Email>();
   List<Lead> totalLeads=[select Company,Email,Phone,Industry,OwnerId,Owner.Name,owner.Email,LastModifiedDate from Lead];
   list<lead>dupes = new list<lead>():
   list<lead>toInsert = new list<lead>():
   for(Lead l1:leads){
        for(Lead l2:totalLeads){
            if(l1.Company==l2.Company && l1.Email==l2.Email && l1.Phone==l2.Phone && l1.Industry==l2.Industry){
                dupes.add(l1);
                messaging.SingleEmailMessage email=new messaging.SingleEmailMessage();
                string[] em= new string[]{l2.owner.Email};
                email.setToAddresses(em);
                email.setSubject('this is test');
                email.setPlainTextBody('this is verification of mail sending');
                emails.add(email);             
            }else{
               toInsert.add(l1);
            }
        }
    }
    insert toInsert;
    if(!emails.isEmpty()) messaging.sendEmail(emails);
    }
}
share|improve this answer
    
if remove addError then duplicate leads can be created? i am avoiding duplicate leads – masthan Mar 26 at 14:16
    
You need to detect the duplicate leads and remove them from your list of leads to insert (remove one or merge them into one lead). Once you add the error, you prevent the DML from executing. Email is a DML operation. You can do your duplicate lead detection in your trigger or another class, then send the emails to the owners in an email handler class. – crmprogdev Mar 26 at 14:18
    
thanks. got it.. is there any other way to achieve this ? – masthan Mar 26 at 14:42

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.