ANS-1421 · CUSTOM FIELDS, RECORDS & FORMS

How to Set a Date Field to the First Day of the Next Month in NetSuite Workflows?

Learn to configure a NetSuite workflow to automatically populate a date field with the first day of the month following the transaction date using SuiteScript 2.x.

Short answer

To set a date field in a NetSuite workflow to the first of the month after the transaction date, implement a custom SuiteScript 2.x function utilizing the N/format module and standard JavaScript Date methods. This function can then be invoked via a custom workflow action or a user event script to dynamically calculate and set the desired date.

Scenario

Users need to automatically populate a specific date field on a NetSuite record with the first day of the month immediately following the record's transaction date. This automation is typically required within a workflow to ensure consistency and reduce manual data entry.

Solution

To set a date field in a NetSuite workflow to the first day of the month after the Transaction Date, it is recommended to use SuiteScript 2.x for new development. While SuiteScript 1.0 functions like nlapiDateToString, nlapiAddDays, nlapiAddMonths, and nlapiStringToDate were historically used in workflow formulas, SuiteScript 2.x provides a more modular and robust approach using the N/format module and standard JavaScript Date object methods. This functionality is typically implemented via a custom workflow action script or a user event script rather than a direct workflow formula.

  1. Create a SuiteScript 2.x Module:

    Develop a SuiteScript 2.x module (e.g., custom_date_utils.js) that contains a function to calculate the desired date. This module will utilize the N/format module for date parsing and formatting, and standard JavaScript Date methods for date manipulation.

javascript/** * @NApiVersion 2.1 * @NModuleScope SameAccount */define(['N/format'], function(format) {    /**     * Calculates the first day of the month following a given date.     * This function is designed to be called from a custom script (e.g., User Event Script or Workflow Action Script)     * to set a date field in NetSuite.     * @param {string} transactionDateString - The transaction date as a string (e.g., from a record field like {trandate}).     * @returns {string} The first day of the next month, formatted as a date string.     */    function getFirstDayOfNextMonth(transactionDateString) {        // 
1. Parse the transaction date string into a Date object        var parsedDate = format.parse({            value: transactionDateString,            type: format.Type.DATE        });        // 
2. Create a new Date object from the parsed date to allow manipulation        var date = new Date(parsedDate);        // 
3. Set the day of the month to 1 to get the first day of the *current* month        date.setDate(1);        // 
4. Add one month to get the first day of the *next* month        date.setMonth(date.getMonth() + 1);        // 
5. Format the resulting Date object back to a string        return format.format({            value: date,            type: format.Type.DATE        });    }    return {        getFirstDayOfNextMonth: getFirstDayOfNextMonth    };});
  1. Implement a Custom Workflow Action Script or User Event Script:

    Create a custom script (e.g., a Workflow Action Script or a User Event Script) that loads the module created in step 1 and calls the getFirstDayOfNextMonth function. This script will then set the value of the target date field on the record.For a Workflow Action Script, you would define an onAction function. Within this function, you would retrieve the transaction date from the current record, call getFirstDayOfNextMonth, and then set the target field.Example snippet for a Workflow Action Script:

javascript/** * @NApiVersion 2.1 * @NScriptType WorkflowActionScript */define(['N/record', 'N/runtime', './custom_date_utils'], function(record, runtime, customDateUtils) {    function onAction(scriptContext) {        var newRecord = scriptContext.newRecord;        var transactionDate = newRecord.getValue({ fieldId: 'trandate' }); // Get the transaction date string        // Calculate the first day of the next month using the custom utility function        var nextMonthFirstDay = customDateUtils.getFirstDayOfNextMonth(transactionDate);        // Set the target date field (replace 'custbody_target_date_field' with your actual field ID)        newRecord.setValue({            fieldId: 'custbody_target_date_field',            value: nextMonthFirstDay        });        // Log for debugging (optional)        log.debug('Calculated Next Month First Day', nextMonthFirstDay);    }    return {        onAction: onAction    };});
  1. Deploy and Integrate with Workflow:

    Deploy your custom script file(s) to NetSuite. Then, configure your workflow to trigger this custom action script at the appropriate state or transition. Ensure the custom action is configured to pass the necessary context (e.g., the current record) to the script. The script will then update the target date field on the record.

Expert NetSuite Support

Need help with this NetSuite issue?

Custom Fields, Records & Forms consulting and configuration support

Talk to a consultant