I have multiple Web-to-Lead forms that gather information for different fields. I want to store all the info under one lead, with the email address being the key. I want to create a trigger to query Leads to see if the email exists. If it does, I want to add this information on to the existing Lead. I am able to find if there is a duplicate email address, but I don't know how to merge the two together.
trigger leadDupeMerge on Lead (before insert) {
Set<String> emails = new Set<String>();
for(Lead ld : trigger.new){
emails.add(ld.Email);
}
emails.remove(null);
for(Lead ld: [SELECT Email FROM Lead WHERE Email = :emails]){
emails.remove(ld.Email);
}
for(Lead ld : Trigger.new){
if(ld.Email != null && emails.contains(ld.Email)){
ld.Email.addError('This lead matches an existing email.');
}
}
}