Back

Unable to convert value to DateTimeOffset when running a Business Central Integration

Published: Mar 17, 2023
Post Author Written by Pat Roth

The user has a SmartConnect.com map that used a Business Central Query using a filter on the query.

DatetimeOffset 1
Because the filter should be dynamic – a user defined global variable, GBL_LASTDATEMODIFIED, was used and initially set as 2023-02-01.

Before Map Task to set the GBL_LASTDATEMODIFIED

var nd = new Date();
nd.setDate(nd.getDate() + parseInt(this.GBL_DELTA_DAY));  //GBL_DELTA_DAY is set to -1
this.GBL_LASTDATEMODIFIED = nd.toISOString().slice(0, 10);

return true;

 

The purpose of this script is to set the date restriction to the current day – 1, in other words, yesterday.

However when running the map or using the Data Source Preview, it fails with the error:

DatetimeOffset 2
Unable to convert value to DateTimeOffset. CorrelationId: 9509xxxxxx

Because we get back a CorrelationId value, we know the error is coming from Business Central and not SmartConnect.com.
What we can’t tell is why we are getting the error.

After further testing including setting the value in the restriction to a hard coded 2021-02-01 vs the global variable used previously – we still received the same error.  The conversion issue wasn’t a code or global variable issue.

A bit more investigation gave us the solution – this custom field is actually defined as a DateTime in BC and not just a Date.  Because we gave it just the date portion of the expected datetime, apparently Business Central didn’t just accept this and so threw this appropriate error.

The solution was to adjust the code to return a proper datetime value instead of just the date as was done previously.

As we were looking for “yesterday” in our code and since we now include the time portion, that actually presents a different problem.  Do we want the time “now” to be yesterday at the same time?  Or at midnight?  It could be either – it depends on how you want to do it.  Both solutions are given in the code block below.

Corrected code for Map Task to set the GBL_LASTDATEMODIFIED

//Get yesterday’s date and set time to midnight by appending on the string for that midnight
var nd = new Date();
nd.setDate(nd.getDate() + parseInt(this.GBL_DELTA_DAY));
this.GBL_LASTDATEMODIFIED = nd.toISOString().slice(0, 10) + ‘T00:00:00.00Z’;
return true;

or

//get the current datetime and set to yesterday at the same time
var nd = new Date();
nd.setDate(nd.getDate() + parseInt(this.GBL_DELTA_DAY));
this.GBL_LASTDATEMODIFIED = nd.toISOString();
return true;

While this example was setting the field restriction for a datetime field on a Business Central query, I would expect this same type of error if we were to be sending a Date value to a DateTime field in Business Central.

Feeling stuck? Get the support and guidance you need to help you power through any data challenge

We're on your integration team. Connect with our people and let us know how we can help you.