Automatically adding the date to transactions is important for accuracy and efficiency in business. It ensures consistency, reduces errors, and simplifies financial reporting. This also helps with compliance by creating a clear audit trail. By using automatic date stamping, businesses can improve workflow, maintain accurate records, and save time, making it a key part of transaction management.
In NetSuite, a SuiteScript 2.x User Event Script can automatically set a custom date field to seven days in the future when a new record is created. This script triggers when the record is initialized, ensuring the date is set without manual input. It helps improve data accuracy, streamline tasks, and reduce errors, which is useful for setting follow-up dates, reminders, or payment deadlines.
Below is an example of how you can write a SuiteScript 2.x:
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
define(['N/record', 'N/error','N/format'], function(record, error, format) {
function beforeLoad(context) {
if (context.type === context.UserEventType.CREATE) {
var currentRecord = context.newRecord;
var newDate = new Date();
newDate = newDate.setDate(newDate.getDate() + 7);
var newDT = format.parse({
value: new Date(newDate),
type: format.Type.DATE,
});
var dt = currentRecord.setValue({ fieldId: 'custbody8',value: newDT });
}
}
return {
beforeLoad: beforeLoad
};
});
Script Breakdown
This User Event Script runs in the beforeLoad event, ensuring the date is pre-populated before the user interacts with the record. Below is a breakdown of the script:
Script Header
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
define(['N/record', 'N/error','N/format'], function(record, error, format) {
The script uses SuiteScript 2.x and is classified as a User Event Script. It imports three NetSuite modules:
- N/record (to interact with records)
- N/error (for error handling, though not used in this script)
- N/format (to format date values properly)
Before Load Event
function beforeLoad(context) {
if (context.type === context.UserEventType.CREATE) {
This function triggers when the record is being created. It checks if the action type is CREATE to ensure the logic runs only for new records.
Setting the Date
var currentRecord = context.newRecord;
var newDate = new Date();
newDate = newDate.setDate(newDate.getDate() + 7);
- Retrieves the record being created.
- Gets the current date.
- Adds seven days to the current date.
var newDT = format.parse({
value: new Date(newDate),
type: format.Type.DATE,
});
- Formats the new date properly using format.parse to match NetSuite's date format.
var dt = currentRecord.setValue({ fieldId: 'custbody8', value: newDT });
- Sets the calculated date into the custom field custbody8.
Returning the Function
return {
beforeLoad: beforeLoad
};
- The function is returned as part of the script’s execution.
Sample Deployment in Sales Order Record
The sample code described herein is provided on an "as is" basis, without warranty of any kind, to the fullest extent permitted by law. Oracle + NetSuite Inc. does not warrant or guarantee the individual success developers may have in implementing the sample code on their development platforms or in using their own Web server configurations.
Oracle + NetSuite Inc. does not warrant, guarantee or make any representations regarding the use, results of use, accuracy, timeliness or completeness of any data or information relating to the sample code. Oracle + NetSuite Inc. disclaims all warranties, express or implied, and in particular, disclaims all warranties of merchantability, fitness for a particular purpose, and warranties related to the code, or any service or software related thereto.
Oracle + NetSuite Inc. shall not be liable for any direct, indirect, or consequential damages or costs of any type arising out of any action taken by you or others related to the sample code.
For more information on handling scripts, refer to the following:
Do you have any other ways to manipulate dates in SuiteScript? Feel free to share them here!