+ Start a Discussion
DevmenDevmen 

Custom Date field is showing wrong time

Hi,

Below is my trigger .Here im trying to create task .Task is having custom duedatefield (Duedate) with time .when i create a task ,Due date should autopopulate in account based on type field in task.Now date is dispalying correct .But there is difference in timing .Timezone  in both user and company information are same.
public class TaskHelper {
    public static void updateTask(List<Task> taskList) {
        Map<Id,Datetime> idActivityDate = new Map<Id,Datetime >();
        Map<Id,Datetime> idActivityDate1=new Map<Id,Datetime>();
        List<id> accountIdList = new List<Id>();
        for(Task temp : taskList) {
            if(String.valueOf(temp.WhatId).subString(0,3) == '001') {
                accountIdList.add(temp.WhatId);
            }
        }

        for(Task temp : [Select Due_DateCustom__c,WhatId,Type,Status From Task where Type='Call' and Status='Completed' and  whatid=:accountIdList and  what.type = 'Account'  order By Due_DateCustom__c DESC limit 1]) {
            system.debug('***********Call'+temp);
          
            idActivityDate.put(temp.WhatId,temp.Due_DateCustom__c); 
            system.debug('IDACTIVITY*************'+ idActivityDate.put(temp.WhatId,temp.Due_DateCustom__c));         
          
           
               
        }
        for(Task temp1: [Select Due_DateCustom__c,WhatId,Type,Status From Task where Type='Email' and Status='Completed' and  whatid=:accountIdList and  what.type = 'Account'  order By Due_DateCustom__c DESC limit 1]) {
            system.debug('***********Email'+temp1);
            
            idActivityDate1.put(temp1.WhatId,temp1.Due_DateCustom__c);                
        }


       List<Account> accList = [Select id, Last_Calls__c,Last_Emails__c  from Account where id in :idActivityDate.keySet() OR id in:idActivityDate1.keySet() ];
        system.debug('ACCOUNTS***************'+accList);
       
        for(Account acc : accList) {
        DateTime acdt=Date.valueOf(idActivityDate.get(acc.id));
       // DateTime acdt1=Date.valueOf(idActivityDate1.get(acc.id));
 
            acc.Last_Calls__c= acdt.addMinutes(30);
             acc.Last_Emails__c =Date.valueOf(idActivityDate1.get(acc.id));
            system.debug('********CALL*******'+ acdt);
            system.debug('********CALL11111111111111*******'+ acc.Last_Calls__c);
             //DateTime acdt1=Date.valueOf(idActivityDate1.get(acc.id));  
          
           // system.debug('********EMAIL1*******'+ acdt1);
            system.debug('********EMAIL*******'+ acc.Last_Emails__c);
        }
       
        update accList;  
        
        
    }       
}

thanks

 
SandhyaSandhya (Salesforce Developers) 
Hi,

The datetime in salesforce is always stored in GMT (UTC). The time is corrected to the users time zone when displaying it on the UI. The time is only corrected in the UI, so you need to make the adjustment yourself for formula fields.

https://salesforce.stackexchange.com/questions/2161/datetime-input-field-cast-to-another-timezone
 
Please mark it as solved if my reply was helpful. It will make it available for other as the proper solution.
                                             
Best Regards
Sandhya