Invalid date error is thrown in one environment an...
# ask-stanley-ai
a
Invalid date error is thrown in one environment and not in another environment
c
AI generated answer (using internal Slack threads):
Based on the conversations, the 'Invalid date error' thrown in one environment and not in another could be due to the way dates are handled in SuiteScript. In SuiteScript, when setting date fields, it is recommended to use a date object created using a string formatted with the user/account's date format preference. For example, if the account date format preference is MM/DD/YYYY, you need to do
new Date(datestring)
where datestring is formatted MM/DD/YYYY. However, there are instances where this might not work as expected due to the internal workings of the NetSuite API. An error might occur when you pass the date to
rec.setValue
or press
rec.save()
. To get around this, you can use the
N/format
module which respects the user/account preference. Here is a code snippet:
Copy code
javascript
var origFmtString = '2019-03-15T14:42:52.022Z';
var origFmtDate = new Date(origFmtString);
var accountFmtString = format.format({ type: format.Type.DATE, value: origFmtDate });
Another approach is to explicitly get the user's date format and use moment.js:
Copy code
javascript
function datestr(d) {
    var fmt = runtime.getCurrentUser().getPreference({
        name: 'DATEFORMAT'
    });
    var m = moment.utc(d);
    return m.isValid() ? m.format(fmt) : '';
}
When setting the date field, it is recommended to use
setValue
with a Date object. If you are using a string, use
setText
. ****** Disclaimer: This response was generated by an artificial intelligence assistant based on previous conversations from multiple Slack channels, and may contain inaccuracies or mistakes. ******
Slack thread 1: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.798776627
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.797776282
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.794392467
Please provide your feedback on this answer.