Jira Code : HRP-5
For Invoice with form ‘COH_ Service Invoice DefRev’, an alert will pop up if the REV REC [END DATE] (custcol1) is less than REV REC [START DATE] (custcol3) on any of the lines in item sublist. Same Start Date and End Date is accepted.
The pop up will say ‘ Please enter correct Rev. Rec. End Date on line. End Date can’t be a date before Start Date.’
This will be triggered when the user tries to save the record. The user can only save the record after correcting the issue. Also, the user cannot proceed by leaving those field empty.
/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
/*******************************************************************************
* * HR Path | HRP - 5 | HRP - 5 Revenue Rec Alert in invoice Record *
* **************************************************************************
*
*
* Author: Jobin & Jismi IT Services LLP
*
* Date Created : 10-April-2019
*
* REVISION HISTORY
*
*
******************************************************************************/
'use strict';
define(['N/currentRecord'], function (currentRecord) {
//To check whether a value exists in parameter
function checkForParameter(parameter, parameterName) {
if (parameter !== null && parameter !== undefined && parameter !== false && parameter !== "null" && parameter !== "undefined" && parameter !== "false" && parameter !== "" && parameter !== " ")
return true;
if (parameterName)
log.debug("Empty Value found", "Empty Value for parameter " + parameterName);
return false;
}
//To assign a default value if the it is empty
function assignDefaultValue(value, defaultValue) {
if (checkForParameter(value)) return value;
return defaultValue;
}
//To reject predefined set of values
function rejectThisValues(value) {
var rejectObj = {
null: true,
undefined: true,
NaN: true,
0: true,
false: true,
"": true
};
return rejectObj[value] ? false : true;
}
//Common Try-Catch function
function applyTryCatch(DATA_OBJ, NAME) {
function tryCatch(myfunction, key) {
return function () {
try {
return myfunction.apply(this, arguments);
} catch (e) {
log.error("error in " + key, e);
// console.error("error in " + key, e);
return false;
}
};
}
for (var key in DATA_OBJ) {
if (typeof DATA_OBJ[key] === "function") {
DATA_OBJ[key] = tryCatch(DATA_OBJ[key], NAME + "." + key);
}
}
}
var dates = {
convert: function (d) {
// Converts the date in d to a date-object. The input can be:
// a date object: returned without modification
// an array : Interpreted as [year,month,day]. NOTE: month is 0-11.
// a number : Interpreted as number of milliseconds
// since 1 Jan 1970 (a timestamp)
// a string : Any format supported by the javascript engine, like
// "YYYY/MM/DD", "MM/DD/YYYY", "Jan 31 2009" etc.
// an object : Interpreted as an object with year, month and date
// attributes. **NOTE** month is 0-11.
return (
d.constructor === Date ? d :
d.constructor === Array ? new Date(d[0], d[1], d[2]) :
d.constructor === Number ? new Date(d) :
d.constructor === String ? new Date(d) :
typeof d === "object" ? new Date(d.year, d.month, d.date) :
NaN
);
},
compare: function (a, b) {
// Compare two dates (could be of any type supported by the convert
// function above) and returns:
// -1 : if a < b
// 0 : if a = b
// 1 : if a > b
// NaN : if a or b is an illegal date
// NOTE: The code inside isFinite does an assignment (=).
return (
isFinite(a = this.convert(a).valueOf()) &&
isFinite(b = this.convert(b).valueOf()) ?
(a > b) - (a < b) :
NaN
);
},
inRange: function (d, start, end) {
// Checks if date in d is between dates in start and end.
// Returns a boolean or NaN:
// true : if d is between start and end (inclusive)
// false : if d is before start or after end
// NaN : if one or more of the dates is illegal.
// NOTE: The code inside isFinite does an assignment (=).
return (
isFinite(d = this.convert(d).valueOf()) &&
isFinite(start = this.convert(start).valueOf()) &&
isFinite(end = this.convert(end).valueOf()) ?
start <= d && d <= end :
NaN
);
}
};
applyTryCatch(dates, "dates");
var main = {
saveRecord: function (scriptContext) {
var invoiceRecord = scriptContext.currentRecord;
var customformId = invoiceRecord.getValue({
fieldId: 'customform'
}).toString().trim();
if (customformId != '101' && customformId != 101) // 101 = COH_ Service Invoice DefRev
return true;
var DATE_MISMATCH_FLAG = false;
var itemLines = invoiceRecord.getLineCount({
sublistId: 'item'
});
var startDate = false,
endDate = false;
for (var index = 0; index < itemLines; index++) {
startDate = invoiceRecord.getSublistValue({
sublistId: 'item',
fieldId: 'custcol3', //Rev Rec [Start Date]
line: index
});
endDate = invoiceRecord.getSublistValue({
sublistId: 'item',
fieldId: 'custcol1', //Rev Rec [End Date]
line: index
});
if (!assignDefaultValue(startDate, false) || !assignDefaultValue(endDate, false)) {
DATE_MISMATCH_FLAG = true;
break;
}
if (dates.compare(dates.convert(startDate), dates.convert(endDate)) == 1) {
DATE_MISMATCH_FLAG = true;
break;
}
}
if (DATE_MISMATCH_FLAG) {
alert("Please enter correct Rev. Rec. End Date on line. End Date can't be a date before Start Date");
return false;
}
return true;
}
};
applyTryCatch(main, "main");
return main;
});