This script is to find the first day of the week from the subsidiary record of the employee
/**
* @NApiVersion 2.1
* @NScriptType UserEventScript
*/
define([‘N/record’, ‘N/search’, ‘N/log’],
/**
* @param {record} record
* @param {search} search
* @param {log} log
*/
(record, search, log) => {
/**
* Function executed before the record is submitted.
* @param {Object} scriptContext
* @param {Record} scriptContext.newRecord – New record
* @param {Record} scriptContext.oldRecord – Old record
* @param {string} scriptContext.type – Trigger type; use values from the context.UserEventType enum
* @since 2015.2
*/
const beforeSubmit = (scriptContext) => {
try {
let objRecord = scriptContext.newRecord;
if (scriptContext.type === scriptContext.UserEventType.CREATE || scriptContext.type === scriptContext.UserEventType.EDIT) {
var employeeId = objRecord.getValue({
fieldId: ‘custrecord_jj_payroll_emp’
});
var subsidiaryId = search.lookupFields({
type: ’employee’,
id: employeeId,
columns: [‘subsidiary’]
}).subsidiary[0].value;
let subsidiaryload = record.load({
type: record.Type.SUBSIDIARY,
id: subsidiaryId,
});
let firstDay = subsidiaryload.getValue({
fieldId: “FIRSTDAYOFWEEK”
});
log.debug(‘FIRSTDAYOFWEEK’, firstDay);
//firstDay =firstDay.getDate()
// Update the first day of the work calendar
objRecord.setValue({
fieldId: ‘custrecord_jj_firstday_workcalender’,
value: firstDay,
});
log.debug(“firstDay”, firstDay);
}
} catch (e) {
log.error({
title: ‘Error in afterSubmit’,
details: e
});
}
}
return {
beforeSubmit
}
});