I have created a trigger that is supposed to avoid a (lead) record being inserted if it contains an email found in another (contact) record. I am able to get the trigger to update (lead) fields if a match is found, however I want it to delete the (lead) record/s instead of inserting them, i.e., I do not want the record/s to be inserted if a match is found. I get the following error:DML statement cannot operate on trigger.new or trigger.old:
below is my trigger code, what I must do to get the trigger to fire successfully on a match?
trigger triggerULDC on Lead(before insert){
map<string,list<Contact>> BObjMap = new map<string,list<Contact>>();
for(Contact contact : [Select Id, Name, email From Contact]){
if(BObjMap.get(contact.email) == null){
BObjMap.put(contact.email, new list<Contact>());
}
BObjMap.get(contact.email).add(contact);
}
List<Lead> listLeadsToDelete = new List<Lead>();
for(Lead lead : trigger.new){
if(BObjMap.containskey(lead.email)){
listLeadsToDelete.add(lead);
continue;
}
}
delete listLeadsToDelete;
}