+ Start a Discussion
sgsssgss 

trigger on object

Can any one help in this? I am new in Trigger. and need a code that cover all the conditions.
System.debug the following statements for any object a)Trigger.New b) Trigger.Old c)Trigger.NewMap d)Trigger.oldMap
Best Answer chosen by sgss
Abdul KhatriAbdul Khatri
Let me know if this help
 
trigger AccountAddressTrigger on Account (before insert, before update, before delete, after insert, after update) {

    if(trigger.isInsert) {
    
    
        if(trigger.isAfter || trigger.isBefore) {    
            for(Account account : trigger.new) {
                
                system.debug(account.Name);
                //More Code
            }
        }
        
        if(trigger.isAfter) {
        
            for(Account account : trigger.newMap.Values()) {
                 system.debug(account.Name);
                //More Code           
                
            }
        }
    
    }
    
    if(trigger.isUpdate) {
    
        for(Account account : trigger.new) {
        
            if(account.Name != trigger.oldMap.get(account.Id).Name) {
                system.debug(account.Name);
                //More Code
            }     
        }
        
        for(Account account : trigger.old) {
        
            if(account.Name != trigger.newMap.get(account.Id).Name) {
                system.debug(account.Name);
                //More Code
            }     
        }  
        
        for(Account account : trigger.oldMap.Values()) {
        
            if(account.Name != trigger.newMap.get(account.Id).Name) {
                system.debug(account.Name);
                //More Code
            }     
        }               
    }
    
    if(trigger.isDelete) {
    
        if(trigger.isBefore) {
            
            for(Account account : trigger.old) {
                system.debug(account.Name);
                system.debug(trigger.oldMap.get(account.Id).Name);
                //More Code            
            }
            
            for(Account account : trigger.oldMap.Values()) {
                system.debug(account.Name);
                //More Code            
            }

            
        }
    
    }
       
}

 

All Answers

Abdul KhatriAbdul Khatri
What is the name of the Object?
What is the requirement for the code to achieve?
sgsssgss
Have to apply Trigger on any Standard/custom object. Consider account.
Abdul KhatriAbdul Khatri
Do you have any scenarion?
sgsssgss
I am new to trigger just want to how these trigger.old trigger.new can be used in various insert update delete event. Any help will be appreciated.
Considering Account object
Abdul KhatriAbdul Khatri
Let me know if this help
 
trigger AccountAddressTrigger on Account (before insert, before update, before delete, after insert, after update) {

    if(trigger.isInsert) {
    
    
        if(trigger.isAfter || trigger.isBefore) {    
            for(Account account : trigger.new) {
                
                system.debug(account.Name);
                //More Code
            }
        }
        
        if(trigger.isAfter) {
        
            for(Account account : trigger.newMap.Values()) {
                 system.debug(account.Name);
                //More Code           
                
            }
        }
    
    }
    
    if(trigger.isUpdate) {
    
        for(Account account : trigger.new) {
        
            if(account.Name != trigger.oldMap.get(account.Id).Name) {
                system.debug(account.Name);
                //More Code
            }     
        }
        
        for(Account account : trigger.old) {
        
            if(account.Name != trigger.newMap.get(account.Id).Name) {
                system.debug(account.Name);
                //More Code
            }     
        }  
        
        for(Account account : trigger.oldMap.Values()) {
        
            if(account.Name != trigger.newMap.get(account.Id).Name) {
                system.debug(account.Name);
                //More Code
            }     
        }               
    }
    
    if(trigger.isDelete) {
    
        if(trigger.isBefore) {
            
            for(Account account : trigger.old) {
                system.debug(account.Name);
                system.debug(trigger.oldMap.get(account.Id).Name);
                //More Code            
            }
            
            for(Account account : trigger.oldMap.Values()) {
                system.debug(account.Name);
                //More Code            
            }

            
        }
    
    }
       
}

 
This was selected as the best answer
sgsssgss
Thanks a lot Abdul