+ Start a Discussion
Nagarjun TNagarjun T 

Trigger to copy account object field to custom object field before delete

Hi everyone,
       before deleting the account iam trying to copy the account data to another custom object, how can i create the trigger can any one help this topic 
Best Answer chosen by Nagarjun T
Nagarjun TNagarjun T
Hello @Piyush & @BramhaihGanta to convert string to decimal we have to use Decimal.valueOf()

trigger accountBackup_NaY on Account (before delete)
 {
        List<Backup_Data__c> lstToInsrt = new List<Backup_Data__c>();  

        for(Account deletedAcc : trigger.old)
        {          
            Backup_Data__c obj = new Backup_Data__c();
              obj.Name = deletedAcc.Name;
              obj.Phone__c = deletedAcc.Phone;
              obj.type__c = deletedAcc.Type;
              obj.Account_Number__c = deletedAcc.accountnumber;
              obj.fax__c = Decimal.valueOf(deletedAcc.Fax);
              

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

All Answers

sfdcMonkey.comsfdcMonkey.com
HI Nagarjun,
   here is the sample trigger code.
trigger accountBackup on Account (before delete) {
    if(trigger.isDelete && trigger.isbefore){
        List<contact> lstToInsrt = new List<contact>();  
        for(Account deletedAcc : trigger.old){
            system.debug('deletedAcc '+deletedAcc );
            contact oCon = new contact();
              oCon.LastName = deletedAcc.Name;
              oCon.FirstName = 'bakcup';   
            lstToInsrt.add(oCon);
            system.debug('oCon '+oCon);
        }
        if(lstToInsrt.size()>0)
        {
            insert lstToInsrt;
            system.debug('list'+lstToInsrt);
        }
    }
}
Thanks,
let us know if it is help you
 
Brahmaiah GantaBrahmaiah Ganta
Hi Nagaraju,
This will be helps you try.
trigger TocreateAnotherRecordWhenBeforeAccDel on Account (before delete) {
    Public List<CustomObjectName__c> recordList = new List<CustomObjectName__c>();
    for(Account acc : Trigger.old){
        CustomObjectName__c ac = new CustomObjectName__c(Name = acc.Name);
        recordList.add(ac);
    }
    if(recordList.size()>0 && recordList != NULL)
   insert recordList;
}

Please Let me Known it helpful or not and make it as best may be helps someone.
Nagarjun TNagarjun T
Hi @Brahmiah Ganta, u mentioned code is working but if i need to copy account name, phone, fax and type on that scenarion how can i create the trigger
sfdcMonkey.comsfdcMonkey.com
in your account backup custom object create custom fields (same data type of account fields) for store deleted account information 
trigger accountBackup on Account (before delete) {
   
        List<CustomObjectName__c> lstToInsrt = new List<CustomObjectName__c>();  

        for(Account deletedAcc : trigger.old){
           
            CustomObjectName__c obj = new CustomObjectName__c();
              obj.Name = deletedAcc.Name;
              obj.Phone__c = deletedAcc.Phone;
			  obj.type__c = deletedAcc.Type;
			  obj.fax__c = deletedAcc.Fax;
             // add all related fields which you want to store 

			lstToInsrt.add(oCon);
        }
        if(lstToInsrt.size()>0){
            insert lstToInsrt;
        }
    
}
Thanks, let us know if it helps
Brahmaiah GantaBrahmaiah Ganta
First we have to create phone, fax and type Fields in Custom Object if not exist than Replace it
CustomObjectName__c ac = new CustomObjectName__c(Name = acc.Name);

with
CustomObjectName__c ac = new CustomObjectName__c(Name = acc.Name,Phone=acc.Phone,Fax=acc..fax);

Like that makw how many columns you want​
Nagarjun TNagarjun T
Hello Thq for ur time @BramaiahGanta and @Piyush getting errors brother
   Error: Compile Error: Illegal assignment from String to Decimal at line 5 column 29
          I have two objects one is 1.Standard Object-- Account
                                                   2.Custom Object -- Backup_Data__C   
    In Backup_Data__c object i created four fields Account name, phone, fax and type so now my question is if i deleted these fields contained record from account object,, before deleting that record i want to store that record in custom Backup_Data__C Object
sfdcMonkey.comsfdcMonkey.com
can you share your code please and also share screenshot of Backup_Data__c object detail page (i need to check data type of custom fields)
Thanks
Nagarjun TNagarjun T
Hello @Piyush 
            Only on fax field iam getting Error: Compile Error: Illegal assignment from String to Decimal at line 12 column 15
                         phone-- phone data type
                          fax -- Number(18, 0) data type
                          type -- picklist data type.


trigger accountBackup_NaY on Account (before delete)
 {
        List<Backup_Data__c> lstToInsrt = new List<Backup_Data__c>();  

        for(Account deletedAcc : trigger.old)
        {          
            Backup_Data__c obj = new Backup_Data__c();
              obj.Name = deletedAcc.Name;
              obj.Phone__c = deletedAcc.Phone;
              obj.type__c = deletedAcc.Type;
              obj.Account_Number__c = deletedAcc.accountnumber;
              obj.fax__c = deletedAcc.Fax;
              

            lstToInsrt.add(obj);
        }
        if(lstToInsrt.size()>0){
            insert lstToInsrt;
        }
    
}
Nagarjun TNagarjun T
Hello @Piyush & @BramhaihGanta to convert string to decimal we have to use Decimal.valueOf()

trigger accountBackup_NaY on Account (before delete)
 {
        List<Backup_Data__c> lstToInsrt = new List<Backup_Data__c>();  

        for(Account deletedAcc : trigger.old)
        {          
            Backup_Data__c obj = new Backup_Data__c();
              obj.Name = deletedAcc.Name;
              obj.Phone__c = deletedAcc.Phone;
              obj.type__c = deletedAcc.Type;
              obj.Account_Number__c = deletedAcc.accountnumber;
              obj.fax__c = Decimal.valueOf(deletedAcc.Fax);
              

            lstToInsrt.add(obj);
        }
        if(lstToInsrt.size()>0){
            insert lstToInsrt;
        }
    
}
This was selected as the best answer
Brahmaiah GantaBrahmaiah Ganta
Hi@Nagaraju,
It is better to delete the fax field in Custom Object if we don't have any data still now in custom object and create new fax field with same data type of "fax field in Account". because may be we will face problems in future like, while matching the fax fields of both Objects etc.