function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Prema -Prema - 

Write a trigger on lead to create a lead but if lead's email exist then show an error.How can i do that pelase give me proper code and make me understand this..Thanks

Best Answer chosen by Prema -
AvaneeshAvaneesh
Hi Prema

Read comment very carefully 
trigger checkEmailTrigger on Lead (before insert) {
// i used before insert trigger because if our condition is true then i have to stop inserting Lead 
//else this it will insert 

   final String errMsg = 'The email already exists on another Lead: '; // this is only for passing message using
  Set< String > emailSet = new Set< String >();
    for( Lead l : Trigger.new ){
        emailSet.add( l.Email );// in the set first i store all the email id which are comming in bulk to set 
    }
  Map< String, Id > duplicateLeadMap = new Map< String, Id >();
  // for bulkyfication of code need a strong logic so i created map where i put email id as key and lead id as      //value because a simple logic which always we used in college day for filtering  
  for( Lead L : [select Id, Email from Lead where Email = :emailSet] )
    duplicateLeadMap.put( L.Email, L.Id );
// after here i start checking the trigger.new values and if email id is in the map simple that mean it exists in 
// my lead object so i put my message in addError function which are used for showing error in apex 
  for( Lead L : Trigger.new ){
    Id duplicateContactId = duplicateLeadMap.get( l.Email );
    if( duplicateContactId != null )
      L.addError( errMsg + duplicateLeadMap );
  }
}

 
if still, you have any problem then let me know Prema

Thank you
Avaneesh Singh

All Answers

AvaneeshAvaneesh
Hi Prema 

This trigger is for and its works fine.
trigger checkEmailTrigger on Lead (before insert) {

   final String errMsg = 'The email already exists on another Lead: ';
  Set< String > emailSet = new Set< String >();
    for( Lead l : Trigger.new ){
        emailSet.add( l.Email );
    }
  Map< String, Id > duplicateLeadMap = new Map< String, Id >();

  for( Lead L : [select Id, Email from Lead where Email = :emailSet] )
    duplicateLeadMap.put( L.Email, L.Id );

  for( Lead L : Trigger.new ){
    Id duplicateContactId = duplicateLeadMap.get( l.Email );
    if( duplicateContactId != null )
      L.addError( errMsg + duplicateLeadMap );
  }
}
Please !!!! don't forget to mark as best answer of my solution 

Thank you
Avaneesh Singh
Prema -Prema -
Hello Avaneesh,

Thanks for the response but I dont know why I am getting following error: 
trigger checkEmailTrigger on Lead (before insert) { final String errMsg = 'The email already exists on another Lead: '; Set< String > emailSet = new Set< String >(); for( Lead l : Trigger.new ){ emailSet.add( l.Email ); } Map< String, Id > duplicateLeadMap = new Map< String, Id >(); for( Lead L : [select Id, Email from Lead where Email = :emailSet] ) duplicateLeadMap.put( L.Email, L.Id ); for( Lead L : Trigger.new ){ Id duplicateContactId = duplicateLeadMap.get( l.Email ); if( duplicateContactId != null ) L.addError( errMsg + duplicateLeadMap ); } }

Thanks.
AvaneeshAvaneesh
Hello Prema

what error did you get can you send me a screen shot or error I don't think so because this work fine in my org 
send me ad let me know.

if get this error that means your trigger is working fine I checked in my org that work fine 

User-added image

Thank you
Avaneesh Singh
Prema -Prema -
Thank you so much ..but can u please explain me what u have written in the code and why you have used ..I would be very grateful to you and surely i will mark your answer as best answer obviously because it helped me alot..
AvaneeshAvaneesh
Hi Prema

Read comment very carefully 
trigger checkEmailTrigger on Lead (before insert) {
// i used before insert trigger because if our condition is true then i have to stop inserting Lead 
//else this it will insert 

   final String errMsg = 'The email already exists on another Lead: '; // this is only for passing message using
  Set< String > emailSet = new Set< String >();
    for( Lead l : Trigger.new ){
        emailSet.add( l.Email );// in the set first i store all the email id which are comming in bulk to set 
    }
  Map< String, Id > duplicateLeadMap = new Map< String, Id >();
  // for bulkyfication of code need a strong logic so i created map where i put email id as key and lead id as      //value because a simple logic which always we used in college day for filtering  
  for( Lead L : [select Id, Email from Lead where Email = :emailSet] )
    duplicateLeadMap.put( L.Email, L.Id );
// after here i start checking the trigger.new values and if email id is in the map simple that mean it exists in 
// my lead object so i put my message in addError function which are used for showing error in apex 
  for( Lead L : Trigger.new ){
    Id duplicateContactId = duplicateLeadMap.get( l.Email );
    if( duplicateContactId != null )
      L.addError( errMsg + duplicateLeadMap );
  }
}

 
if still, you have any problem then let me know Prema

Thank you
Avaneesh Singh
This was selected as the best answer