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 

delete record by trigger

Hi Expert, 

I am using a trigger that will fire to delete any record but when  record delete then new screen appears
"The record you attempted to access has been deleted. The user who deleted this record may be able to recover it from the Recycle Bin. Deleted data is stored in the Recycle Bin for 15 days."

But here i don't need this message.
Can any one plesae suggest.

Thanks
Mukesh 
Best Answer chosen by mukesh gupta
BALAJI CHBALAJI CH
Not sure, but my understanding of your requirement is, When a New Contact is beoing inserted with a Email which is already there in ane xisting contact., then the new contact should be updated with the values of existing contact and existing contact should be deleted.
As per this understanding., please see below Trigger:
Trigger NewContact on Contact (before insert) {
    Set<String> con = new Set<String>();
    List<Contact> cont = new  List<Contact>();
    
    for (Contact c : Trigger.New) 
    {
        con.add(c.Email);
    }
    
    Map<string, Contact> Map1 = new Map<string, Contact>();
    for(Contact c: [SELECT Id,Email,Phone,Title
                    FROM Contact
                    WHERE Email IN :con])
    {
        Map1.put(c.Email, c);
    }
    
    if(Map1.size() > 0) 
    {
        for (Contact c : Trigger.New) 
        {
            if(Map1.containsKey(c.email))
            {
                contact c1 = Map1.get(c.Email);
                
                c.Phone = c1.phone;
                c.Title = c1.Title;
                
                cont.add(c1);
            }
        }
        delete cont;
    }
}

Try this and let me know if this is what you are looking for.

Best Regards,
BALAJI
 

All Answers

SaketJoshiSaketJoshi
Looks like your trigger is going into recursion and trying to delete the same record that had been deleted earlier. Could you post the code snippet for better understanding of the problem?
BALAJI CHBALAJI CH
Hi Mukesh,

I think after deleting the record in the Trigger, might be you are trying to do some modification on the record in the trigger. 
Please check in the trigger if there are any actions performing on the recrod after the delete DML.

Best Regards,
BALAJI
mukesh guptamukesh gupta
In my trigger, I want to delete existing contact if new contact comes with same Email Id.

Thanks
Mueksh

 
BALAJI CHBALAJI CH
The way you are deleting the exisiting contact might be the reason for this. Make sure that the contact is not being deleted multiple times.
If you can share your trigger, we can try to troubleshoot.
mukesh guptamukesh gupta
I am new for this
I want to update existing contact whose email Id is same with existing contact. afetr match the email with old to new then update contact.

this is below code :-

Trigger NewContact on Contact (before insert) {
    Set<Id> con = new Set<Id>();

 List<Contact> cont = new  List<Contact>();
 
    for (Contact c : Trigger.New) {
        con.add(c.id);
    }

    List<Contact> existing_contact = [SELECT Id,Email,Phone,Title
                              FROM Contact
                              WHERE Email IN :con];

    if(existing_contact.size() > 0) {
        for (Contact c : existing_contact) {
            
           // c.Phone = Trigger.New[0].Phone;
            //c.Title = Trigger.New[0].Title;
           // cont.add(c);
           
        }
        //update cont;
    }
}
BALAJI CHBALAJI CH
Not sure, but my understanding of your requirement is, When a New Contact is beoing inserted with a Email which is already there in ane xisting contact., then the new contact should be updated with the values of existing contact and existing contact should be deleted.
As per this understanding., please see below Trigger:
Trigger NewContact on Contact (before insert) {
    Set<String> con = new Set<String>();
    List<Contact> cont = new  List<Contact>();
    
    for (Contact c : Trigger.New) 
    {
        con.add(c.Email);
    }
    
    Map<string, Contact> Map1 = new Map<string, Contact>();
    for(Contact c: [SELECT Id,Email,Phone,Title
                    FROM Contact
                    WHERE Email IN :con])
    {
        Map1.put(c.Email, c);
    }
    
    if(Map1.size() > 0) 
    {
        for (Contact c : Trigger.New) 
        {
            if(Map1.containsKey(c.email))
            {
                contact c1 = Map1.get(c.Email);
                
                c.Phone = c1.phone;
                c.Title = c1.Title;
                
                cont.add(c1);
            }
        }
        delete cont;
    }
}

Try this and let me know if this is what you are looking for.

Best Regards,
BALAJI
 
This was selected as the best answer
mukesh guptamukesh gupta
Hi BALAJI,

One more thing about this trigger is that, i not want to delete existing contact, but update with new contact with email, meand contact will not delete only update with existing if email id is same.

waiting for possitive reply

Many Thanks 
Mukesh
BALAJI CHBALAJI CH
Here, we are New Contact's Values with Existing Contact's values.
So, you mean to say that Existing Contact's values should be updated with New Contact's values and no need to delete Existing Contact. Right ??
mukesh guptamukesh gupta
Yes, you are right, shouldn't insert new contact only update existing 

Thanks
Mukesh