Suitescript to Format Date according to the Date Format set in the NetSuite Account

define([‘N/log’, ‘N/config’, ‘N/format’],

    /**

 * @param{format} format

 * @param{log} log

 */

    (log, config, format) => {

        /**

         * Defines the Scheduled script trigger point.

         * @param {Object} scriptContext

         * @param {string} scriptContext.type – Script execution context. Use values from the scriptContext.InvocationType enum.

         * @since 2015.2

         */

        const execute = (scriptContext) => {

            try {

                // Fetch the user-level preferences configuration

                let userPreferences = config.load({

                    type: config.Type.USER_PREFERENCES

                });

                // Get the date format used in the account

                let dateFormat = userPreferences.getValue({

                    fieldId: ‘DATEFORMAT’

                });

                log.debug(‘Account Date Format’, dateFormat);

                // Define the due date (e.g., 20th of the current month)

                let today = new Date();

                let reqDate = new Date(today.getFullYear(), today.getMonth(), 20);

                // Format the due date based on the fetched date format

                let formattedReqDate = format.format({

                    value: reqDate,

                    type: format.Type.DATE,

                    format: dateFormat

                });

                log.debug(‘Formatted Date’, formattedReqDate);

            }

            catch(e){

                log.debug(‘Error@execute’, e.message + n + e.stack);

            }

        }

        return { execute }

    });

Leave a comment

Your email address will not be published. Required fields are marked *