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
MiramoMiramo 

before delete trigger to save information to custom object

Hi,

I am trying to write a before delete trigger on a custom object to capture information before its deleted and then save it to another custom object and getting errors.
I am not a developer but having ago (badly so far).  Here is what I have and the errors: -

Trigger so far for deletion

trigger deleteproject on Project__c (before delete)
 {
        List<Deleted_Records__c> lstToInsrt = new List<Deleted_Records__c>(); 

        for(Deleted_Records__c deletedProj : trigger.old)
        {         
       
            Deleted_Records__c pd = new Deleted_Records__c();
              pd.Object_Name__c = 'Project';
              pd.Object_Record_ID__c = Project__c.Id;
              pd.Date_Deleted__c = DateTime.NOW();
              pd.Deleted_Record_Owner__c = Project__c.OwnerId.Name;
              pd.Who_Deleted__c = getFirstName() + getLastName();
              pd.Project_Name__c = Proect__c.Name;
              pd.Project_Code__c = Project__c.Project_Code__c;
              pd.Region__c = Project__c.Region__c;
              pd.Project_Active_or_Dormant__c = Project__c.Project_Active_or_Dormant__c;
              pd.ERP_ID__c = Project__c.ERP_ID__c;
              pd.Object_Contact__c = Project__c.Project_Contact1__c;
        

            lstToInsrt.add(pd);
        }
        if(lstToInsrt.size()>0){
            insert lstToInsrt;
        }
   
}

I am getting the error Illegal assignment from Schema.SObjectField to String at line 10 column 15.

The field Object_Record_Id__c is a text field.
I am sure I will get a similar error for the fields Who_Deleted__c and also Deleted_Record_Owner__c
sachinarorasfsachinarorasf
Hi Miramo,

According to your code, you are writing a trigger on the 'Project__c ' object.
trigger deleteproject on Project__c (before delete)

But in below line of code:
 for(Deleted_Records__c deletedProj : trigger.old) 
 You are using Deleted_Records__c which is inappropriate. Because trigger.old will return the list of the object Project__c. So, please try to use the below line of code:
  for(Project__c deletedProj : trigger.old) 

I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com
MiramoMiramo
Ahh yes thanks, have amended that.

I get an error Compile Error: Illegal assignment from Schema.SObjectField to String at line 10 column 15

thats this line pd.Object_Record_ID__c = Project__c.Id;
sachinarorasfsachinarorasf
Hi Miramo,

I have modified your code. Please look into the below lines of code and execute it.
trigger deleteproject on Project__c (before delete)
 {
        List<Deleted_Records__c> lstToInsrt = new List<Deleted_Records__c>(); 

        for(Project__c deletedProj : trigger.old)
        {         
       
            Deleted_Records__c pd = new Deleted_Records__c();
              pd.Object_Name__c = 'Project';
              pd.Object_Record_ID__c = deletedProj.Id;
              pd.Date_Deleted__c = DateTime.NOW();
              pd.Deleted_Record_Owner__c = deletedProj.OwnerId.Name;
              pd.Who_Deleted__c = getFirstName() + getLastName();
              pd.Project_Name__c = deletedProj.Name;
              pd.Project_Code__c = deletedProj.Project_Code__c;
              pd.Region__c = deletedProj.Region__c;
              pd.Project_Active_or_Dormant__c = deletedProj.Project_Active_or_Dormant__c;
              pd.ERP_ID__c = deletedProj.ERP_ID__c;
              pd.Object_Contact__c = deletedProj.Project_Contact1__c;
        

            lstToInsrt.add(pd);
        }
        if(lstToInsrt.size()>0){
            insert lstToInsrt;
        }
   
}
I hope you find the above solution helpful. If it does, please mark it as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
www.sachinsf.com