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
RadramRadram 

Error on Trigger - Trigger to check any existing contacts based on the Email of Lead

Hello All, 

 

The trigger checks the Email of the new Lead with the existing Emails and gives the Names of Contacts and Accounts with the same Email ID on the lead Page. Its not working properly as the related list is now showing all the contacts and accounts even if there is no Email in the lead.

 

Please help me on this. Thanks. 

Here is my trigger: 

 


trigger LeadRelatedList on Lead (after insert,after update) {

set<String> leads=new set<String>();
for(Lead l: Trigger.new){
leads.add(l.Email);
System.debug('Lead  Name ####'+l.Email);
}

List <Contact> listcon=[select ID,AccountId from Contact Where Email IN:leads];
System.debug('Matched Contacts   ####'+listcon.size());
Set <Id> ids=new Set<ID>();
for(Contact con: listcon){
ids.add(con.accountid);
}
System.debug('Matched  Account s #######'+ids.size());
for(Lead l:Trigger.new){
for(Contact con : listcon){
con.Lead__c= l.id;
}
}
update listcon;
}

 

srinu_SFDCsrinu_SFDC

You may try to print the Set to see what in there.

I think its empty space which is causing the problem.

RadramRadram

Hello Srinu, 

 

I'm new to Programming. Could please tell me how to print the Set?

 

Thanks a lot. 

srinu_SFDCsrinu_SFDC

You just put the below statement before you fetch contact info.

System.debug('All lead emails -->' + leads);