record.submitFields throws Invalid date when I use...
# ask-stanley-ai
a
record.submitFields throws Invalid date when I use JS date object. What is the work around or resolution for this?
c
AI generated answer (using internal Slack threads):
Based on the conversations, it seems that the issue arises when trying to set a date value using a JavaScript Date object in SuiteScript API 2.1. The error 'Invalid date value (must be D.M.YYYY)' is thrown when the date format does not match the user/account's date format preference. Here are a few solutions that were suggested: 1. Avoid formatting the date: If you are pulling the date from another NetSuite field, you do not need to format it. Simply set the value as it is. 2. Use the
N/format
module
: This module respects the user/account preference. You can use it to format the date before setting the value. 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 });
3. Get the user's date format and use moment.js: This approach involves getting the user's date format and using moment.js to format the date. Here is a code snippet:
Copy code
javascript
function datestr(d) {
    var fmt = runtime.getCurrentUser().getPreference({
        name: 'DATEFORMAT'
    });
    var m = moment.utc(d);
    return m.isValid() ? m.format(fmt) : '';
}
Remember, the date format preference can vary between users and accounts, so it's important to respect this preference when setting date values. ****** 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.8451491
Slack thread 2: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.83548522
Slack thread 3: here (available only if the original thread was posted in the last 90 days)
Similarity score: 0.83181417
Please provide your feedback on this answer.