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
Rakesh K 60Rakesh K 60 

Save data with delete/update/create

Hello Experts,

I need a quick confirmation with my code. The requirement is like this.

 1. When I delete/create/update an Account
 2. There is this object called track
 3. I want this object to have the Account Name with type.
trigger TrackMethod on Account (before delete) {
        List<Track__c> tList = new List<Track__c>();
        if(Trigger.isBefore)
        { 
            if(Trigger.isDelete){
                for (Account m : [SELECT Id, Name FROM Account where Id IN :Trigger.old]) {
                    Track__c t = new Track__c();
                    t.Name=m.name;
                    t.action__c='Deleted';
                    t.type__c='Account';
                    tList.add(t);
                }
            }
        }
    else if(Trigger.isAfter) {
            for(Account m:trigger.New){
                Track__c t = new Track__c();
                t.Name=m.name;
                if(Trigger.isInsert){
                    t.action='Created';
                } else if(Trigger.isUpdate){
                    t.action='Updated';
                }
                t.type__c='Method';
                tList.add(t);
            }
        }
        insert tList;
    }
Please let me know if my code is correct.

Thanks
Abdul KhatriAbdul Khatri

Hi Rakesh,

Looks like there is no relationship (Lookup or Master Detail) between Account and Track SObject. Is there any reason of doing that.

Is the Track SObject is to track multiple other SObject beside Account?

Is Type a picklist values? If Yes, what are the possible values.
When Account get's deleted I see you still retrieving Account Name but account might have gone so still taging the Track with the deleted account doesn't make sense.

I did a little cleanup with the code, but still need to look at the delete part based on my above question

trigger TrackMethod on Account (before delete, after insert, after update) 
{   
    List<Track__c> tList = new List<Track__c>();
    switch on Trigger.OperationType  
    {
        when BEFORE_DELETE 
        {
            for (Account m : [SELECT Id, Name FROM Account where Id IN :Trigger.old]) 
            {
                Track__c t = new Track__c();
                t.Name=m.name;
                t.action__c='Deleted';
                t.type__c='Account';
                tList.add(t);
            }            
        }
        when AFTER_INSERT, AFTER_UPDATE
        {
            for(Account m:trigger.New){
                Track__c t = new Track__c();
                t.Name=m.name;
                t.action = Trigger.isInsert ? 'Created' : 'Updated'
                t.type__c='Method';
                tList.add(t);
            }            
        }    
    }
    
    insert tList;
}

Let me know

CharuDuttCharuDutt
Hii Rakesh
Try Below Code
 
trigger AccountBackup on Account (After insert,After update,Before delete) {
   List<Track__c> tList = new List<Track__c>();
    if(Trigger.IsAfter && Trigger.IsAfter){
        for(Account Acc: Trigger.new){
             Track__c t = new Track__c();
                    t.Name=Acc.name;
                    t.action__c='Created';
                    t.type__c='Account';
                     tList.add(t);
          
        }
    }
    
    
     if(Trigger.IsAfter && Trigger.Isupdate){
        for(Account Acc: Trigger.new){
            Track__c t = new Track__c();
                    t.Name=Acc.name;
                    t.action__c='Updated';
                    t.type__c='Account';
                     tList.add(t);
        }
         
    }
    
	if(Trigger.IsBefore && Trigger.IsDelete){
        for(Account Acc : trigger.old){          
		 Track__c t = new Track__c();
                    t.Name=Acc.name;
                    t.action__c='Deleted';
                    t.type__c='Account';
                     tList.add(t);
        }
       if( tList.Size()>0){
            insert  tList;
        }
      
}
Please Mark it As Best Answer If It Helps
Thank You!