ANS-1230 · SUITESCRIPT DEVELOPMENT
How to Resolve ‘Invalid Field Value’ When Setting Date Fields in NetSuite Scripts?
This guide explains how to correctly set date and time values in NetSuite custom records using SuiteScript 2.x, avoiding common formatting errors.
Short answer
To resolve the 'Invalid Field Value' error when setting a date field in NetSuite SuiteScript, ensure you use formatModule.parse instead of formatModule.format when converting a date object to a string for field assignment. The format method is for displaying, while parse is for preparing values for record fields.
Scenario
A user attempting to set a date/time field on a NetSuite custom record via SuiteScript may encounter an 'Invalid Field Value' error. This typically occurs when a script tries to assign a date string to a field using an incorrect formatting method, leading to a mismatch between the expected field type and the provided value.
Solution
The 'Invalid Field Value' error arises when the formatModule.format method is incorrectly used to prepare a date string for a record field. The format method is intended for displaying dates, not for setting field values. To correctly set a date/time field, the formatModule.parse method should be used instead.The problematic code snippet is shown below:
var formattedDateString = formatModule.format({value: new Date(), type: formatModule.Type.DATETIME});settlementRecord.setValue({fieldId: 'custrecord_gs_settlement_dateprocessed', value: formattedDateString});This results in the error:You have entered an Invalid Field Value 11/28/2018 3:45:53 pm for the following field: custrecord_gs_settlement_dateprocessedThe correct approach is to use formatModule.parse to ensure the date string is in a format acceptable for the NetSuite field. The corrected line should be:
var formattedDateString = formatModule.parse({value: new Date(), type: formatModule.Type.DATETIME});By changing .format to .parse, the script will correctly process and set the date value for the custrecord_gs_settlement_dateprocessed field.
Expert NetSuite Support
Need help with this NetSuite issue?
SuiteScript Development consulting and configuration support
