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 

can anyone help to solve this problem??

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

}


I am gertting error last line..
Illegal assignment from Long to Datetime 
this is the error i am getting.. please help me to solve this querry.. and plzz provide me correct code

Thank you!!
Rathindra Dakua 13Rathindra Dakua 13
You are getting that error because of this line:-  
ts.Actual_Time__c=(system.now().getTime()-ts.TaskOpened__c.getTime()/(1000*60*60));

According to this doc - https://developer.salesforce.com/docs/atlas.en-us.apexref.meta/apexref/apex_methods_system_datetime.htm#apex_System_Datetime_getTime, return type of getTime() is "Long". I am assuming that the Actual_Time__c field is of type DateTime. That is the reason why the compiler can not assign a Long value to the DateTime field.
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you check if Actual_Time__c is of type Number and 
TaskOpened__c is of type Date/Time.

This might be causing the issue 

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
atharva Vispute 3atharva Vispute 3
Hi,

Both actual time and task opened fields are of datetime data type.. plz provide me correct line of code.
Thanks!!
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Atharva,

As the field should capture the differenece of datetime fields it will capture in Hours .

Thanks,
 
atharva Vispute 3atharva Vispute 3
Hi,

So what can I do now? Please provide me correct  line of code

Thanks!!