+ Start a Discussion
himanshu huske 7himanshu huske 7 


Please help me with simple code...
A trigger that will prevent a user from creating a lead that already exists as a contact. We will use the lead /contact email address to detect duplicates.
Lead is created or updated.
Lead has an email address.
2. Try to find a matching contact based on email address.(Using SOQL)
If a match is found give an error
 If a match is not found do nothing
Best Answer chosen by himanshu huske 7
Hi Himanshu,

Try below code,
trigger preventDuplicateLead on Lead (before insert) {
	set<string> setEmailId = new set<string>();
	for(Lead leadObj : trigger.new) {
	list<Contact> lstContact = [SELECT Email FROM Contact WHERE Email IN : setEmailId];
	map<string,Contact> mapContact = new Map<string,Contact>();
	for(Contact conObj : lstContact) {
	for(Lead leadObj : trigger.new) {
		If(mapContact.containsKey(leadObj.Email)) {
			leadObj.addError('Already exist');
I did this in notepad, apologies for any syntax error.

Let us know, if this helps.