Scenario:
How to restrict project selection in a weekly time sheet record if it is not allocated to a specific employee for a particular month
Solution
/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
/************************************************************************************************
* CLIENT NAME : International Planned Parenthood Federation
* IPPF- 1228 Restriction based on Project Allocation record
*
*********************************************************************************************
*
* Author: Jobin and Jismi IT Services
* Date Created : 27-April-2023
* Created By: Aswathy, Jobin and Jismi IT Services
* Description : Script for Restriction based on Project Allocation recordr
*
* REVISION HISTORY
* Revision 1.0 - 27 April 2023 - Restriction based on Project Allocation record
*
***********************************************************************************************/
define(['N/currentRecord', 'N/record', 'N/search','N/format'],
/**
* @param{currentRecord} currentRecord
* @param{record} record
* @param{search} search
* @param{format} format
*/
function(currentRecord, record, search,format) {
/**
* Function to check the Project Allocation
* @param customer
* @returns {*[]}
*/
function searchProjectAllocation(customer)
{
try
{
var customrecord_jj_project_allocSearchObj = search.create({
type: "customrecord_jj_project_alloc",
filters:
[
["custrecord_jj_project","anyof",customer],
"AND",
["isinactive","is","F"]
],
columns:
[
search.createColumn({name: "custrecord_jj_project", label: "Project"}),
search.createColumn({name: "custrecord_jj_project_period", label: "Payroll Record"})
]
});
var searchResultCount = customrecord_jj_project_allocSearchObj.runPaged().count;
var payRollArray =[];
if(searchResultCount > 0)
{
customrecord_jj_project_allocSearchObj.run().each(function(result){
payRollArray.push(result.getValue({
name: "custrecord_jj_project_period", label: "Payroll Record"
}))
return true;
});
console.log("payRollArray",payRollArray)
return payRollArray;
}
else
{
return []
}
}
catch(err)
{
console.log("error@searchProjectAllocation",err)
return [];
}
}
/**
* Funtion to check the availability of a payroll record
* @param checkProjectAllocation defines the payroll internal id
* @param employee defines employee name
* @param monthsArray defines the start month and end month
* @param yearArray defines the start year and end year
* @returns {boolean}
*/
function payRollAllocationSearch(checkProjectAllocation,employee,monthsArray,yearArray)
{
try
{
var customrecord_jj_payroll_detailsSearchObj = search.create({
type: "customrecord_jj_payroll_details",
filters:
[
["custrecord_jj_payroll_emp","anyof",employee],
"AND",
["custrecord_jj_month","anyof",monthsArray],
"AND",
["custrecord_jj_year","is",yearArray],
"AND",
["internalid","anyof",checkProjectAllocation]
],
columns:
[
search.createColumn({name: "custrecord_jj_monthly_rate", label: "Monthly Rate"}),
search.createColumn({name: "custrecord_jj_month", label: "Month"}),
search.createColumn({name: "custrecord_jj_year", label: "Year"})
]
});
var searchResultCount = customrecord_jj_payroll_detailsSearchObj.runPaged().count;
if(searchResultCount > 0)
{
return true
}
else
{
return false;
}
}
catch(err)
{
console.log("error@payRollAllocationSearch",err)
return false;
}
}
/**
* Function for check for parameter
* @param parameter
* @returns {boolean}
*/
const checkForParameter = function checkForParameter(parameter) {
if (parameter !== "" && parameter !== null && parameter !== undefined && parameter !== false && parameter !== "null" && parameter !== "undefined" && parameter !== " " && parameter !== 'false' && parameter !== 0 && parameter !== '0') {
return true;
}
}
/**
* Function to be executed after page is initialized.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.mode - The mode in which the record is being accessed (create, copy, or edit)
*
* @since 2015.2
*/
function pageInit(scriptContext) {
}
/**
* Function to be executed when field is slaved.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
* @param {string} scriptContext.fieldId - Field name
*
* @since 2015.2
*/
function postSourcing(scriptContext) {
try
{
if(scriptContext.fieldId == 'customer')
{
var currentRecord = scriptContext.currentRecord;
var recordType = currentRecord.type;
var sublistName = scriptContext.sublistId;
var sublistFieldName = scriptContext.fieldId;
var weekDay = currentRecord.getValue({
fieldId:'trandate'
})
var employee = currentRecord.getValue({
fieldId:"employee"
});
var weekDayDate = new Date(weekDay);
var startMonth = (weekDayDate.getMonth()) + 1
var startYear = weekDayDate.getFullYear();
var lastWeekDayDate = new Date(weekDayDate.setDate(weekDayDate.getDate() + 7))
var gmtLastWeekDayDate = format.format({
value: lastWeekDayDate,
type: format.Type.DATETIME,
timezone: format.Timezone.GMT
});
var endMonth = gmtLastWeekDayDate.split('/');
var endMonthValue = endMonth[1];
var endYear = (endMonth[2]).split(' ')
var endYearValue = endYear[0];
var monthsArray=[];
var yearArray=[];
var flag= true;
var flagValue =true;
monthsArray.push(startMonth,endMonthValue);
yearArray.push(startYear,endYearValue);
if(recordType === 'timesheet') {
if( sublistName == 'timeitem' && sublistFieldName == 'customer') {
var customer = currentRecord.getCurrentSublistValue({
sublistId: 'timeitem',
fieldId: 'customer',
});
if(!customer)
{
return false;
}
var checkProjectAllocation = searchProjectAllocation(customer);
if(checkProjectAllocation.length > 0)
{
var checkPayRoll = payRollAllocationSearch(checkProjectAllocation,employee,monthsArray,yearArray);
if(checkPayRoll == false || checkPayRoll == 'false')
{
alert("You do not have permission to choose the project since the project allocation record for this month has not been created.")
currentRecord.setCurrentSublistValue({
sublistId: 'timeitem',
fieldId: 'customer',
value: ''
})
flag = false
return false;
}
}
else
{
alert("You do not have permission to choose the project since the project allocation record for this month has not been created.")
currentRecord.setCurrentSublistValue({
sublistId: 'timeitem',
fieldId: 'customer',
value: ''
})
flagValue = false
return false;
}
}
}
}
}
catch(err)
{
console.log("error@postSourcing",err)
}
}
return {
pageInit: pageInit,
postSourcing:postSourcing
};
});