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
Vikram Singh 157Vikram Singh 157 

Hi! My requirement is :- Avoid insertion of any lead if that record already exist as contact in my organisation.Match Email id of contact with lead], below trigger gives error

trigger leadrecords on lead(before insert){
list<string> leadEmails = new list<string>();
for(Lead lead:Trigger.new){

leadEmails.add(lead.Email);
}
List<Contact> leads = [SELECT Id, Email FROM  Contact WHERE Email IN :leadEmails];
for (Lead newlead : leads){
if (newlead.email == leadEmails){
system.debug('Contsct is already exist');
}

else {
insert lead.Email;
}
}

 
Best Answer chosen by Vikram Singh 157
HARSHIL U PARIKHHARSHIL U PARIKH
Try using the following trigger:
 
Trigger DupeLeadRecord On Lead(Before Insert, Before Update){
    
    If(Trigger.IsInsert || Trigger.IsUpdate){
        For(Lead Ld : Trigger.New){
            If(Ld.Email != null)
            {
                List<Contact> sameEmailCons = [Select FirstName, LastName, Email FROM Contact WHERE Email =:Ld.Email LIMIT 1];
                
                If(sameEmailCons.Size() > 0){
                    Ld.addError('Same Email Address Contact Exists In Our Org Whoes Name Is: ' + sameEmailCons[0].FirstName + sameEmailCons[0].LastName);
                }
            }
        }
    }
}

If it solves the puzzle then please mark it as best answer and it will help others with similar issue!
 

All Answers

HARSHIL U PARIKHHARSHIL U PARIKH
Try using the following trigger:
 
Trigger DupeLeadRecord On Lead(Before Insert, Before Update){
    
    If(Trigger.IsInsert || Trigger.IsUpdate){
        For(Lead Ld : Trigger.New){
            If(Ld.Email != null)
            {
                List<Contact> sameEmailCons = [Select FirstName, LastName, Email FROM Contact WHERE Email =:Ld.Email LIMIT 1];
                
                If(sameEmailCons.Size() > 0){
                    Ld.addError('Same Email Address Contact Exists In Our Org Whoes Name Is: ' + sameEmailCons[0].FirstName + sameEmailCons[0].LastName);
                }
            }
        }
    }
}

If it solves the puzzle then please mark it as best answer and it will help others with similar issue!
 
This was selected as the best answer
Vikram Singh 157Vikram Singh 157
Thanks Mr. Harshil Parikh AKA:Govind, it works , actually just now i starts working on triggers so there are be some confusion to do things in different different logics .
HARSHIL U PARIKHHARSHIL U PARIKH
No problem VIkram, It's my pleasure to help on communities!
BTW, if you are new and willing to learn more about trigger, logics etc.. I would really recommend watching David Liu's videos on pluralSight.

Link = https://app.pluralsight.com/profile/author/david-liu

Hope this helps!