X++ date addition issue


I came across this issue with adding dates to negative integers. Basically the issue is that you cant add a negative value to a date variable (but you can subtract a positive integer).
i.e. if x = -5, and date1 is 01/01/2012,
and you do date1 = date1 + x,
this results in a runtime error which says “Error executing code”. This error is not even caught by the catch block.

Following is a job i wrote to prove this.


static void DateOperatorIssue(Args _args)
{
    Date date1,date2,date3;
    int dateDiff;
    ;
    try
    {
        date1 = mkdate(01,01,2013);
        date2 = mkdate(10,01,2013);
        date3 = mkDate(15,01,2013);
        dateDiff = date3 - date2;
        info(strfmt("Date diff is: %1", dateDiff)); //dateDiff = 5
        date1 += dateDiff;                          //This works (You can only add positive integers, or subtract positive integers)
        info(strfmt("New date is: %1", date1));
        dateDiff = date2 - date3;
        info(strfmt("Date diff is: %1", dateDiff)); //dateDiff = -5
        date1 += dateDiff;                          //Runtime error: Error executing code: Wrong type of argument for conversion function.
        info(strfmt("New date is: %1", date1));
    }
    catch(Exception::Error)
    {
        error('ooooops');                           //This is never reached
}
}

Advertisements

Leave a Reply / Comment

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s