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
mukesh guptamukesh 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 
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) {
   
}
    
  
}

 
Vivian Charlie 1208Vivian Charlie 1208

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

v varaprasadv varaprasad
Hi Mukesh,


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.
Lokesh KumarLokesh Kumar
Hi Mukesh Please go through the below Link and do an upsert instead of insert 

http://developer.force.com/cookbook/recipe/preventing-duplicate-records-from-saving 


Thanks,
Lokesh
 
Shivdeep KumarShivdeep Kumar
Hi Mukesh,

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