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
atharva Vispute 3atharva Vispute 3 

plzz explain me this trriger code

trigger TaskTimeCalculation on Task (before insert,before update) {
    
    if(Trigger.isinsert && Trigger.isbefore)
    
    for (Task t:Trigger.new){
        if(t.status=='New')
            t.TaskOpened__c =system.now();
    }
    
    if(Trigger.isupdate && Trigger.isbefore){
        for(Task ts:Trigger.new){
        
        Task oldtask= Trigger.oldmap.get(ts.id);
            
            if(ts.Status=='New' && oldtask.Status!='new'){
                ts.TaskOpened__c=system.now();
            }
            
            if(ts.Status=='Completed' && oldtask.Status!='Completed' && ts.TaskOpened__c!=null ){
                
                
             ts.Actual_Time__c=(system.now().getTime()-ts.TaskOpened__c.getTime()) /(1000*60*60);
            }
            }
    }

}

Can anyone explain me this code ??
AnkaiahAnkaiah (Salesforce Developers) 
Hi Atarva,

When you insert the task record and the status= new then you were updating the TaskOpened__c field with current time.

When you update the task record and the status field old value not equals to new  && current value of status= new then you were updating the TaskOpened__c field with current time.

When you update the task record and the status field = completed && old value not equals to completed  && TaskOpened__c field should not empty  then you were updating the Actual_Time__c field with current time - TaskOpened__c  time  .

If this helps, Please mark it as best answer.

Thanks!!


 
atharva Vispute 3atharva Vispute 3
HI,
Ankaiah 

Thank you it really helps me.
Thanks!!