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

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:{
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){
System.debug('Matched  Account s #######'+ids.size());
for(Contact con : listcon){
update listcon;



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

I think its empty space which is causing the problem.


Hello Srinu, 


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


Thanks a lot. 


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

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