Rounding DateTime as part of Web Service call

nick_robbonick_robbo Member Posts: 49
Hi,

As part of our solution, we have an application which sends Web Service calls to NAV in order to Clock In/Clock Out Employees.
When an entry is made in the table, we validate the "Event Time" and then calculate an "Amended Time" in the table trigger.
This second time is in essence a rounded time, taking into account grace periods for lateness/overtime.

For example, if I am meant to start work at 7AM, and clock in at 7:04AM, the system will calculate the "Amended Time" as 7AM.
We are seeing some instances, where this is incorrectly being rounded to 7:30AM.

Yet, if I re-validate the field "Event Time" without changing the value...it calculates correctly.
This happens for every example we get.

I have run some scripts to insert say 100 rows into the table (from a codeunit), and error if the time is incorrectly rounded. Which frustratingly passes every time.

I guess my question is, how could this be happening....the value of the field runs through the same logic, with the same value...and comes out with different answers.

Has anyone encountered issues like this before, in relation to Web-Service calls?

Best Answer

  • nick_robbonick_robbo Member Posts: 49
    Answer ✓
    I have found the issue,

    The XML was passed correctly to the Web Service, but the issue is this line
    ExpectedClockInDateTime := CREATEDATETIME(DT2DATE(EventDateTime), ExpectedClockInTime);
    

    CREATEDATETIME is adding an hour on the to time which we specify in the variable ExpectedClockInTime.
    So this needs to be converted to UTC time.

Answers

  • nick_robbonick_robbo Member Posts: 49
    edited 2017-08-24
    Here is the function which is called...... which is passed the following parameters "EmployeeNo, EventDateTime) Returns - DateTime
    GracePeriod := 240000; // 4 mins
    RoundingTimePeriod := 1800000; // 30 mins
    EventDateTime := ROUNDDATETIME(EventDateTime, 60000); // round to minute
    
    ExpectedClockInTime := GetExpectedClockInTime(EmployeeNo, EventDateTime);
    IF ExpectedClockInTime = 0T THEN
      EXIT(EventDateTime); // if no expected, exit without rounding
    ExpectedClockInDateTime := CREATEDATETIME(DT2DATE(EventDateTime), ExpectedClockInTime);
    
    IF CheckTimeWithinGracePeriod(GracePeriod, EventDateTime, ExpectedClockInDateTime) THEN 
      EXIT(ExpectedClockInDateTime);
    
    IF EventDateTime < ExpectedClockInDateTime THEN
      EXIT(GetAmendedTimeForEarlyClockIn(EventDateTime,
        ExpectedClockInDateTime, GracePeriod, RoundingTimePeriod))
    ELSE IF EventDateTime > ExpectedClockInDateTime THEN
      EXIT(GetAmendedTimeForLateClockIn(EventDateTime,
        ExpectedClockInDateTime, GracePeriod, RoundingTimePeriod))
    ELSE
      EXIT(EventDateTime);
    

    The examples which are causing us a problem should exit at this line -
    IF CheckTimeWithinGracePeriod(GracePeriod, EventDateTime, ExpectedClockInDateTime) THEN 
      EXIT(ExpectedClockInDateTime);
    

    Which is why it is so strange that when I re-validate the field, which calls this code...it does exit correctly at this point.
  • nick_robbonick_robbo Member Posts: 49
    Answer ✓
    I have found the issue,

    The XML was passed correctly to the Web Service, but the issue is this line
    ExpectedClockInDateTime := CREATEDATETIME(DT2DATE(EventDateTime), ExpectedClockInTime);
    

    CREATEDATETIME is adding an hour on the to time which we specify in the variable ExpectedClockInTime.
    So this needs to be converted to UTC time.
  • AntHillMobAntHillMob Member Posts: 79
    Have a look at the settings on your service tier. There is a setting called 'Service Default Time Zone'. Depending on the value this can adjust times in web service calls depending on timezones. Might not be your issue but worth checking.
Sign In or Register to comment.