+ Start a Discussion
Amit GoyalAmit Goyal 

Trigger Issue: DateTime_Field = Date_Value + Integer_Value; = Expected_Result - 1

I have found a issue or whatever which is not clear to me.
I have a trigger on Account on Before Insert and Before update events. and code is as follows.
DateTime_Field__c field is a DateTime field. My System date is 1/23/2015 2:24 PM.

trigger AccountAddressTrigger on Account (before insert, before update) {
    for(Account acc: Trigger.New){
        acc.DateTime_Field__c = System.today() + 7;
    }
}

Issue:
When I am assigning System.Now() + 7, Its assigning the date 1/30/2015 4:00 PM
When I am assigning System.Today() + 7, Its assigning the date 1/29/2015 4:00 PM

I tried the same with my other laptop and other developer account, with System.Today() + 2, and System.Now() + 2
The DateTime_Field = System.Today() + Integer_Value; always assigning the date Expected - 1.

What could be the reason.
James LoghryJames Loghry
The correct syntax to use would be
acc.Date_Time_Field__c = Date.today().addDays(7);
Amit GoyalAmit Goyal
I know the correct syntax is Any date/datetime field's addDays method.
but my question is why the above scenario is working or if working giving the wrong result.