Populate Description field with the user first name who creates or updates the record by using userInfo standard keyword , and also does not allow user to delete the record. Write it's Test class also.

Example : If user is Test User "Author Last updated by user firstName (use userInfo to fetch firstname)"
....TRIGGER....
trigger PopulateDescription on Account (before insert, before update, before delete){
    If(trigger.isInsert)
    {
        for(Account Acc: Trigger.new){
            Acc.Description = 'Account Created by '+ userInfo.getFirstName(); 
        }
        
    } else
        If(trigger.isUpdate)
    {
        for(Account Acc: Trigger.new){
            Acc.Description = 'Account Last updated by '+ userInfo.getFirstName();   
        }
    }
    
    if(Trigger.isDelete&&Trigger.isbefore){ 
        for(Account Rec:trigger.old) 
        {
            
            Rec.adderror('You Cannot Delete the Account Record');
        }
    }
}
....Test Class....

Comments

Popular posts from this blog

Write a trigger and it's Test class on Account when Account is update check all opportunity inside the account.