ANS-0870 · SUITESCRIPT DEVELOPMENT
How to Set a Custom Date Field Value in NetSuite SuiteScript 2.x
Learn the correct SuiteScript 2.x method for programmatically updating custom date fields on NetSuite records and ensuring changes are saved.
Short answer
To set a custom date field in SuiteScript 2.x, use `record.setValue({ fieldId: 'your_field_id', value: new Date() })` on a loaded or created record object. Always remember to save the record using `record.save()` after setting field values to persist changes, as forgetting to submit the record will prevent changes from being applied.
Scenario
A user needs to programmatically update a custom date field on a NetSuite record using SuiteScript. They are encountering issues with correctly setting the field value and ensuring the changes are saved to the record.
Solution
The process for setting a custom date field value in SuiteScript 2.x involves loading or creating the target record, using the record.setValue(options) method with the correct data type, and then saving the record to persist the changes.
First, obtain a reference to the record object. This is typically achieved by loading an existing record using
record.load()or creating a new one withrecord.create()from theN/recordmodule.To set the value of a custom date field, use the
record.setValue()method. It is crucial to provide a JavaScriptDateobject as the value; attempting to set a string (e.g., 'hello') to a date field will result in anINVALID_FLD_VALUEerror.After setting the field value, the record must be explicitly saved using
record.save()to apply the changes to NetSuite.The following SuiteScript 2.x example demonstrates how to load a record, set a custom date field (custevent_assignee_email_date) to the current date, and then save the record:
javascript/** * @NApiVersion 2.x * @NScriptType UserEventScript */define(['N/record', 'N/log'], function(record, log) { function afterSubmit(context) { if (context.type === context.UserEventType.CREATE || context.type === context.UserEventType.EDIT) { try { // Load the current record in dynamic mode to easily set values var currentRecord = record.load({ type: context.newRecord.type, id: context.newRecord.id, isDynamic: true }); // Set the custom date field 'custevent_assignee_email_date' with a Date object // Note: Setting a string like 'hello' to a date field will cause an INVALID_FLD_VALUE error. currentRecord.setValue({ fieldId: 'custevent_assignee_email_date', value: new Date() // Sets the field to the current date and time }); // Save the record to persist the changes var recordId = currentRecord.save(); log.debug('Record Update', 'Record ID: ' + recordId + ' updated with new date for custevent_assignee_email_date.'); } catch (e) { log.error('Error setting custom date field', e.toString()); } } } return { afterSubmit: afterSubmit };});Expert NetSuite Support
Need help with this NetSuite issue?
SuiteScript Development consulting and configuration support
