This is the code sample to restrict the weekends and holidays from the work calendar of the employee in the track time record
/**
* @NApiVersion 2.1
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
define([‘N/record’, ‘N/search’], function (record, search) {
function formatDate(date) {
const formattedDate = `${date.getDate()}/${date.getMonth() + 1}/${date.getFullYear()}`; // Format date as D/M/YYYY
return formattedDate;
}
function getWorkCalendarDetails(employeeId, tranDate) {
try {
console.log(‘getWorkCalendarDetails – employeeId:’, employeeId);
console.log(‘getWorkCalendarDetails – tranDate:’, tranDate);
let month = tranDate.getMonth() + 1;
let year = tranDate.getFullYear();
const searchResult = search.create({
type: “customrecord_jj_payroll_details”,
filters: [
[“custrecord_jj_payroll_emp”, “anyof”, employeeId],
“AND”,
[“isinactive”, “is”, “F”],
“AND”,
[“custrecord_jj_month”, “is”, month],
“AND”,
[“custrecord_jj_year”, “is”, year]
],
columns: [
search.createColumn({ name: “custrecord_jj_wrk_calendar”, label: “Work Calendar” }),
search.createColumn({ name: “custrecord_jj_firstday_workcalender”, label: “First Day” })
]
}).run().getRange({ start: 0, end: 1 });
console.log(“searchResult.length”, searchResult.length);
if (searchResult.length > 0) {
const result = searchResult[0];
const workCalendar = result.getValue({ name: “custrecord_jj_wrk_calendar” });
const subsidiaryFirstDay = result.getValue({ name: “custrecord_jj_firstday_workcalender” });
const holidays = [];
const exceptionDescriptions = [];
const workcalendarSearchObj = search.create({
type: “workcalendar”,
filters: [
[“internalid”, “anyof”, workCalendar]
],
columns: [
search.createColumn({ name: “exceptiondate”, label: “Exception Date” }),
search.createColumn({ name: “exceptiondescription”, label: “Exception Description” })
]
});
workcalendarSearchObj.run().each(function (result) {
const exceptionDate = result.getValue({ name: “exceptiondate” });
holidays.push(exceptionDate);
exceptionDescriptions.push(result.getValue({ name: “exceptiondescription” }));
return true;
});
console.log(‘getWorkCalendarDetails – holidays:’, holidays);
console.log(‘getWorkCalendarDetails – exceptionDescriptions:’, exceptionDescriptions);
return {
workCalendar: workCalendar,
subsidiaryFirstDay: subsidiaryFirstDay,
holidays: holidays,
exceptionDescriptions: exceptionDescriptions
};
}
return false;
} catch (err) {
console.log(‘Error@getWorkCalendarDetails’, err);
log.error(‘Error@getWorkCalendarDetails’, err);
return false;
}
}
function restrictHolidays(currentObj, trackDate, holidays, exceptionDescriptions) {
try {
console.log(‘restrictHolidays – trackDate:’, trackDate);
console.log(‘restrictHolidays – holidays:’, holidays);
const formattedTrackDate = formatDate(trackDate);
const dayOfWeek = trackDate.getDay();
let isHoliday = holidays.includes(formattedTrackDate);
console.log(“Condition values”, {
isHoliday: isHoliday,
dayOfWeek: dayOfWeek,
});
if (isHoliday || dayOfWeek === 0 || dayOfWeek === 6) {
currentObj.getField({
fieldId: ‘hours’
}).isDisabled = true;
} else {
currentObj.getField({
fieldId: ‘hours’
}).isDisabled = false;
}
} catch (err) {
console.log(‘Error in restrictHolidays:’, err);
log.error(‘Error in restrictHolidays:’, err);
return false;
}
}
const restrictHolidays = () => {
}
function fieldChanged(scriptContext) {
try {
restrictHolidays()
} catch (e) {
console.log(‘Error in fieldChanged:’, e);
log.error(‘Error in fieldChanged:’, e);
return false;
}
}
return {
fieldChanged: fieldChanged
};
});