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
IvanB@LMIvanB@LM 

Trigger - on delete insert into another table

Hi, Experts - 

 

Could some one please drop a few snippets on how it would look to have a after delete trigger, so when a row is deleted on the triggered object deleted row gets archived (inserted) into another custom object ?

 

Thanks,

Navatar_DbSupNavatar_DbSup

Hi,

 

Use the below code as reference

 

trigger insertondelete on account(before delete)
{
list<customobject__c>cus=new list<customobject__c>();
for(account aa:trigger.old)
{
customobject__c cs1=new customobject__c(name=aa.name);
cus.add(cs1);
}
if(cus.size()>0)
insert cus;
}

 

Did this answer your question? If not, let me know what didn't work, or if so, please mark it solved. 


cloudmaniacloudmania

From the perspective of your case,It would be better if Trigger is  on before delete .

trigger UrTrigger on UrObject (before delete) { 

   list<urObjectToArchieve> MyList=new list< urObjectToArchieve>();

   for(UrObject obj:Trigger.New)

   {

       /*******retrieve ur field which you want to archieve****/

      urObjectToArchieve  temp=new  urObjectToArchieve (Name=obj.Name,,....); 

         MyList.add(temp);

   } 

  //DML on object for acrhieve 

   Database.insert(  MyList);

}

IvanB@LMIvanB@LM

Thank you for the contribution. I am very new to this; maybe one day I will be able to contribute to the community !!!

 

 

IvanB@LMIvanB@LM

This worked in direct table row deletions; however when parent row is deleted and child rows deleted as a result then trigger did not fire. Trigger is on child object. Any ideas ?

 

Thanks,

dmchengdmcheng

You need a BeforeDelete trigger on your parent object that retrieves the child records and inserts them.