4

I have a field Last_Call_c date and has a trigger but the date request is a day early.

My code

trigger CallDate on Task (after insert, after update, after delete) 
{
    Set<Id> con_set = new Set<Id>();
    List<Contact> con_list = new List<Contact>();
    for( Task T: Trigger.new )
    {
        // if(String.valueof(T.whoid).startsWith('003') && T.Status=='Completed' && T.Subject=='Call' )
        {
            con_set.add(T.whoid);
        }
    }

    for(AggregateResult aggregateResult:[SELECT max(createdDate)MaxCDate,whoid FROM Task WHERE whoid IN: con_set AND Status ='Completed' AND (subject LIKE 'call%' OR subject LIKE 'outbound%') group By whoid])
    {
        con_list.add(new Contact(Id=(id)aggregateResult.get('whoid'),Last_Call__c=date.valueof(aggregateResult.get('MaxCDate'))));

    }
    try
    {
        if(con_list !=null && con_list.size()>0)
        {
            update con_list;
        }
    } Catch(Exception ee){
        system.debug('Exception ***'+ee.getMessage());
    }
}

Any idea?

Thanks

5
  • issue may be with timeZone – Ratan Paul Feb 24 '16 at 8:07
  • unfortnately, thats the first thing I looked at. I think its in the trigger. I just dont know what causing it because I look at the activity history date that's right. It's this trigger maybe. – Ninja King Feb 24 '16 at 8:11
  • 1
    Did you try to use the convertTimezone() function? SELECT max(convertTimezone(createdDate))MaxCDate – Martin Lezer Feb 24 '16 at 8:26
  • 1
    I agree with Ratan and crmprogdev that this is probably a timezone issue. This trigger will execute in the context of whoever created that task. Now, my question is: who is creating the task? Is it the same end user who sees the date being off by a day, or is the task being created by some other automated process (ie: via partner WSDL). In the latter case, the user for the automated process may have a different timezone to the end user and that would cause your dates to be off (especially if the difference is big). – mkorman Feb 25 '16 at 16:55
  • @MartinLezer I tried that and I got this error. Error: Compile Error: timezone conversion only applies to date aggregate functions at line 13 column 42 – Ninja King Feb 25 '16 at 20:34
3

You're querying the createdDate which is a DateTime field, then converting it to a Date field. From the SOQL and SOSL reference:

SOQL queries in a client application return dateTime field values as Coordinated Universal Time (UTC) values. You can use convertTimezone() in a date function to convert dateTime fields to the user’s time zone.

As such, without doing the Timezone conversion before you do the Date conversion, that would very easily cause your dates to be a day off; depending of course on your location and the time of day for the records.

3
  • I tried using the convertTimezone() but is currently giving me error. – Ninja King Feb 25 '16 at 20:36
  • Error: Compile Error: timezone conversion only applies to date aggregate functions at line 13 column 42 – Ninja King Feb 25 '16 at 20:36
  • From the error message, it sounds like you did that afterward instead of in your query? At least that sounds like what the message is telling you. You need to use a different method to convert it outside of your query. – crmprogdev Feb 25 '16 at 20:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.