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
BittusBittus 

using apex triggers how to create a new contact record with values of account when the account is deleted

hi all...
         actually when we delete an account, i need to create a new contact with the details of deleted account. how can we do this using apex triggers
Sameer Tyagi SFDCSameer Tyagi SFDC
Hi Bittus, 

Here is the sample code. 
trigger CreateAccountContact on Account (after delete) {
      list<contact> conlist = new list<contact>();
      for(account acc : trigger.old){
             // enter your contact information here
             contact con = new contact();
             con.lastname = acc.name ;
             con.phone = acc.phone;
            conlist.add(con);
      
      }
      if(conlist.size()>0){
              insert conlist;
      }

}

Best Regards, 
Sameer Tyagi 
www.mirketa.com
BittusBittus
thanq for ur reply.. its working for the account and contact objects. but its not working for my custom objects. the code is here: trigger tgr_updatetax on Customer__c (after delete) { list conlist = new list(); for(customer__c acc : trigger.old){ test__c con = new test__c(); con.name = acc.name ; con.phone__c = acc.phone__c; con.salary__c = acc.salary__c; con.Billingcity__c = acc.Billing_city__c; conlist.add(con); } if(conlist.size()>0){ insert conlist; } }
Sameer Tyagi SFDCSameer Tyagi SFDC
trigger tgr_updatetax on Customer__c (after delete) { 
       list<test__c> conlist = new list<test__c>();
       for(customer__c acc : trigger.old){  
             test__c con = new test__c(); 
             con.name = acc.name ; 
             con.phone__c = acc.phone__c; 
             con.salary__c = acc.salary__c; 
             con.Billingcity__c = acc.Billing_city__c; 
             conlist.add(con); 
       } 
      if(conlist.size()>0){ 
            insert conlist; 
      } 
}
Correct the highlighted line in your above code, It will work 

Sameer Tyagi
www.mirketa.com
BittusBittus
ya i tried it but its giving the error like " There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Apex trigger tgr_Tax caused an unexpected exception, contact your administrator: tgr_Tax: execution of AfterDelete caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Customer]: [Customer]: Trigger.tgr_Tax: line 14, column 1". "
Sameer Tyagi SFDCSameer Tyagi SFDC
There is required field on test__c object, in which you need to mention the id of any Customer record, 

I think you are trying to delete the Parent object Customer__c, and creating a record Child record Test__c, 

and there is Master detail relationship in Customer__c and Test__c 

During creation a record of Test__c, you need to set id of parent object. 

con.Customer__c = //  set id here 

NOTE : you can not set the id of same Customer__c which you are deleting. 

Sameer Tyagi 



 
BittusBittus
am not getting how to do that. can u plz help
 
Suraj Tripathi 47Suraj Tripathi 47
Hi Bittus,

You can take reference from the below code, it will help you.

trigger CreateContact on Account (after delete) {
      List<contact> contactList = new List<contact>();
      for(account acc : trigger.old){
             contact conObj = new contact();
             conObj.lastname = acc.name ;
             conObj.phone = acc.phone;
            contactList.add(conObj);
      
      }
      if(!contactList.isEmpty()){
              insert contactList;
      }

}


If you find your Solution then mark this as the best answer. 


Thank you!

Regards 
Suraj Tripathi