• Kumar__Gaurav
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies

My scenario is to show an error message if lead as duplicate email ID. In lead i have three email field .Email, Alternate email 1, Alternate email 2 should be checked to keep the email ids unique. For e.g. If an email has been entered as a primary email id then it cannot be entered as a primary or as an alternate email id for another contact and the vice versa. 

I wrote a code but its not working. Can anyone help me out to solve this.
 

trigger DuplicateLead on Lead(before insert, before update) {
 Map<String, Id> mapLeads = NEW Map<String, String>();
 for(Lead l : [SELECT Email, Alternate_Email1__c,Alternate_Email_2__c FROM Lead]) {
  if(l.Email != NULL) {
   mapLeads.put(l.Email, l.Id);
  }
  if(l.Alternate_Email1__c != NULL) {
   mapLeads.put(l.Alternate_Email1__c , l.Id);
  }
  if(l.Alternate_Email_2__c != NULL) {
   mapLeads.put(l.Alternate_Email_2__c, l.Id);
  }
 }
 for(Lead l : Trigger.NEW) {
  Boolean flag = FALSE;
   if ((l.Email==l.Alternate_Email1__c) || (l.Email==l.Alternate_Email_2__c) || (l.Alternate_Email1__c== l.Alternate_Email_2__c))
  if (mapLeads.containsKey(l.Email)) {
   flag = TRUE;
  }
  if (mapLeads.containsKey(l.Alternate_Email1__c )) {
   flag = TRUE;
  }
  if (mapLeads.containsKey(l.Alternate_Email_2__c )) {
   flag = TRUE;
  }
  if(flag) {
  l.addError('Error:Provided email id already exist for another record');
  }
 }
Thanks In Advance