/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define([‘N/record’, ‘N/format’, ‘N/config’, ‘N/search’],
(record, format, config, search) => {
const beforeSubmit = (scriptContext) => {
try {
// Helper function to format date as M/D/YYYY
function formatDate(date) {
return `${date.getMonth() + 1}/${date.getDate()}/${date.getFullYear()}`;
}
// Helper function to check if a date is a weekend (Saturday or Sunday)
function isWeekend(date) {
let day = date.getDay(); // 0 = Sunday, 6 = Saturday
return (day === 0 || day === 6);
}
// Helper function to check if a date is a holiday (from holidays array)
function isHoliday(date, holidaysArray) {
let formattedDate = formatDate(date);
return holidaysArray.includes(formattedDate);
}
// Function to get the next valid business day (not a weekend or holiday)
function getNextBusinessDay(date, holidaysArray) {
let nextDate = new Date(date);
nextDate.setDate(nextDate.getDate() + 1); // Move to the next day
// Keep moving forward if the next day is a weekend or a holiday
while (isWeekend(nextDate) || isHoliday(nextDate, holidaysArray)) {
nextDate.setDate(nextDate.getDate() + 1); // Move to the next day
}
return nextDate;
}
if (scriptContext.type === ‘create’) { // Trigger only on record creation
let newRecordObj = scriptContext.newRecord;
// Load company configuration for timezone information
let confg = config.load({
type: config.Type.COMPANY_INFORMATION
});
let tz = confg.getValue({ fieldId: ‘timezone’ });
// Format the current date in the company’s timezone
let estFormattedDate = format.format({
value: new Date(),
type: format.Type.DATETIME,
timezone: format.Timezone[tz] // Set to company’s timezone
});
// Parse the formatted date back into a Date object in the same timezone
let estDateObj = format.parse({
value: estFormattedDate,
type: format.Type.DATETIME,
timezone: format.Timezone[tz] // Ensure it stays in the correct timezone
});
// Perform a search for holidays
let customrecord_jj_holidays_calendarSearchObj = search.create({
type: “customrecord_jj_holidays_calendar”,
columns: [
search.createColumn({ name: “custrecord_jj_holiday_date”, label: “Date” }),
search.createColumn({ name: “custrecord_jj_holiday_description”, label: “Description” })
]
});
let holidaysArray = [];
customrecord_jj_holidays_calendarSearchObj.run().each(function (result) {
holidaysArray.push(result.getValue({ name: “custrecord_jj_holiday_date” }));
return true;
});
// Format the current date
let formattedDate = formatDate(estDateObj);
let nextDate = getNextBusinessDay(estDateObj, holidaysArray);
// Get the shipping location
let shipLocation = newRecordObj.getText({ fieldId: ‘location’ });
// Extract hours and minutes from the parsed EST date object
let currentHours = estDateObj.getHours();
let currentMinutes = estDateObj.getMinutes();
// Check if today is a holiday and update the ship date to the next business day
if (holidaysArray.includes(formattedDate)) {
newRecordObj.setValue({
fieldId: ‘shipdate’,
value: nextDate
});
newRecordObj.setValue({
fieldId: ‘memo’,
value: ‘Ship date updated due to holiday’
});
return; // Exit as ship date has been updated
}
// Handle shipping conditions based on location and time
let shipDateUpdated = false;
if (shipLocation === ‘CHPBOS’) {
if (currentHours >= 12) {
newRecordObj.setValue({
fieldId: ‘shipdate’,
value: nextDate
});
shipDateUpdated = true;
}
} else if (shipLocation === ‘CHPLAX’) {
if (currentHours >= 15) {
newRecordObj.setValue({
fieldId: ‘shipdate’,
value: nextDate
});
shipDateUpdated = true;
}
} else if (shipLocation === ‘CHPCHI’) {
if (currentHours >= 13) {
newRecordObj.setValue({
fieldId: ‘shipdate’,
value: nextDate
});
shipDateUpdated = true;
}
}
// Add a note to the memo field if the ship date is updated
if (shipDateUpdated) {
newRecordObj.setValue({
fieldId: ‘memo’,
value: ‘Ship date updated based on location and time cut-off’
});
}
}
} catch (e) {
log.error(‘Error in beforeSubmit’, e);
}
}
return { beforeSubmit };
});