You need to sign in to do that
Don't have an account?
mukesh gupta
trigger problem for delete
Hi,
if a contact is already add with an email id , and user try to insert a new contact with existing email id then old contact details with update with new contact after that new contact will delete.
this is my code
if a contact is already add with an email id , and user try to insert a new contact with existing email id then old contact details with update with new contact after that new contact will delete.
this is my code
Trigger NewContact on Contact (before insert) { Set<String> con = new Set<String>(); List<Contact> cont = new List<Contact>(); List<Id> duplicateIds = new List<Id>(); Map<string, Contact> existing = new Map<string, Contact>(); for (Contact c : Trigger.New) { con.add(c.Email); } for(Contact c: [SELECT Id,Email,Phone,Title FROM Contact WHERE Email IN :con]) { existing.put(c.Email, c); } if(existing.size()>0){ for (Contact c : Trigger.New) { if(existing.containsKey(c.Email)){ duplicateIds.add(c.id); Contact clone = c.clone(); clone.Id = existing.get(c.Email).Id; existing.put(c.Email, clone); } } } try { update existing.values(); Database.delete(duplicateIds); } catch (System.DmlException e) { } }
Hi Mukesh,
In before insert you will be unable to delete the new record that is being inserted since this has not been commited to the database. One option available here is that you can use the object.addError() method for new records that you find as duplicate. This way they would not be inserted in the system itself, but then you will not even be able to copy the records details from the new record to the existing record.
The other option you have is to use an after insert trigger. Find the existing records, copy values from trigger.new to the existing records and then delete the records from trigger.new that you found as duplicates.
Thanks
Vivian
please try once below code.
Trigger NewContact on Contact (after insert) {
Set<String> con = new Set<String>();
List<Contact> cont = new List<Contact>();
List<contact> duplicateIds = new List<contact>();
Map<string, Contact> existing = new Map<string, Contact>();
for (Contact c : Trigger.New)
{
con.add(c.Email);
}
for(Contact c: [SELECT Id,Email,Phone,Title
FROM Contact
WHERE Email IN :con])
{
existing.put(c.Email, c);
}
if(existing.size()>0){
for (Contact c : Trigger.New)
{
if(existing.containsKey(c.Email)){
duplicateIds.add(new contact(id = c.id));
Contact clone = c.clone();
clone.Id = existing.get(c.Email).Id;
existing.put(c.Email, clone);
}
}
}
try {
update existing.values();
Database.delete(duplicateIds);
} catch (System.DmlException e) {
}
}
please let me know is tis corrct or not.
Regards
VaraPrasad.
http://developer.force.com/cookbook/recipe/preventing-duplicate-records-from-saving
Thanks,
Lokesh
Please take a look on below link, as per my understanding it will help you.
https://salesforce.stackexchange.com/questions/17847/what-is-the-best-way-to-delete-a-set-of-records-during-insert
Please let me know if this help.
Thanks
Shivdeep