In NetSuite, handling user preferences like date formats is crucial when processing or displaying date-sensitive information. This article discusses how to retrieve and apply the user’s preferred date format in a User Event Script (UES) and perform custom logic based on the format.
runtime.getCurrentUser().getPreference()Theruntimemodule provides details about the currently logged-in user. UsinggetPreference(), you can retrieve specific preferences, likeDATEFORMAT, which dictates how dates are formatted for the user.- Converting Dates Based on the User’s Preference After fetching the user’s preferred date format, the script can perform date conversions to standardize the format. In this example, if the format is
MM/DD/YYYY, the script converts a date (dateCreatedd) into the matching format. Otherwise, it leaves the original date as is.
define(['N/record', 'N/log', 'N/search', 'N/ui/serverWidget', 'N/runtime'],
function (record, log, search, serverWidget, runtime) {
function beforeLoad(scriptContext) {
try {
// Fetching the current user's date format preference
let userDateFormat = runtime.getCurrentUser().getPreference({
name: 'DATEFORMAT'
});
// If the user's date format is MM/DD/YYYY, convert the date
if (userDateFormat == 'MM/DD/YYYY') {
log.debug("User prefers MM/DD/YYYY format");
// Create a Date object from the original date string
let dateObject = new Date(dateCreatedd);
// Validate the date object
if (!isNaN(dateObject.getTime())) {
let month = ("0" + (dateObject.getMonth() + 1)).slice(-2);
let day = ("0" + dateObject.getDate()).slice(-2);
let year = dateObject.getFullYear();
// Format the date as MM/DD/YYYY
let dateCreated = month + '/' + day + '/' + year;
} else {
log.error("Date Conversion Error", "The provided date format could not be parsed.");
}
} else {
// Use the original date string if the format isn't MM/DD/YYYY
let dateCreated = dateCreatedd;
}
} catch (e) {
log.error("Error in beforeLoad", e.toString());
}
}
return {
beforeLoad: beforeLoad
};
});