VALIDATE 2 DATES LIE BETWEEN 2 DATES OR NOT || OVERLAPPING CASE ALSO
Mon Dec 11 2023 13:12:52 GMT+0000 (Coordinated Universal Time)
Saved by @mdfaizi #javascript
/**
* /**
*@NApiVersion 2.1
*@NScriptType ClientScript
*/
define(["N/format", "N/search"], function (format, search) {
validateLine = (context) => {
try {
var recObj = context.currentRecord;
var returnValue = true;
var alertMessage;
if (context.sublistId == "recmachcustrecord_hcg_employee3") {
//get current start and end date, and compare with all the lines start and end date wether it line between those date or not
var CurrentLineIndex = recObj.getCurrentSublistIndex({
sublistId: "recmachcustrecord_hcg_employee3",
});
log.debug("CurrentLineIndex", CurrentLineIndex);
var currentLineStartDate = recObj.getCurrentSublistValue({
sublistId: "recmachcustrecord_hcg_employee3",
fieldId: "custrecord_hcg_start_date6",
}); //get current start date
var currentLineEndDate = recObj.getCurrentSublistValue({
sublistId: "recmachcustrecord_hcg_employee3",
fieldId: "custrecord_hcg_end_date6",
}); //get current end date
log.debug("current currentLineStartDate ", currentLineStartDate + " currentLineEndDate " + currentLineEndDate);
var currentLineStartDate = new Date(currentLineStartDate);
var currentLineEndDate = new Date(currentLineEndDate);
log.debug("current currentLineStartDate ", currentLineStartDate + " currentLineEndDate " + currentLineEndDate);
var lineCount = recObj.getLineCount({
sublistId: "recmachcustrecord_hcg_employee3",
});
log.debug("lineCount", lineCount);
for (var i = 0; i < lineCount; i++) {
if (i != CurrentLineIndex) {
//current editing index should not be compared. else it would be comparing same index values that would satisfy the condition.
log.debug("inside loop i=", i);
var startDateLine = recObj.getSublistValue({
sublistId: "recmachcustrecord_hcg_employee3",
fieldId: "custrecord_hcg_start_date6",
line: i,
}); //get start date of line
var endDateLine = recObj.getSublistValue({
sublistId: "recmachcustrecord_hcg_employee3",
fieldId: "custrecord_hcg_end_date6",
line: i,
}); //get end date of line
startDateLine = new Date(startDateLine);
endDateLine = new Date(endDateLine);
log.debug("startDateLine ", startDateLine + " endDateLine " + endDateLine);
//if current start date and current end date is in between the existing startDateLine and endDateLine then give alert
if (
(currentLineStartDate >= startDateLine && currentLineEndDate <= endDateLine) || //current start date and current end date lies between any already present start date and end date.
(currentLineStartDate >= startDateLine && currentLineStartDate <= endDateLine) || //Current start date lies between any already present start and end date.(this means date is overlapping)
(currentLineEndDate >= startDateLine && currentLineEndDate <= endDateLine) // Current end date lies between any already present start and end date.(this means date is overlapping)
) {
//|| (currentLineEndDate >= startDateLine && currentLineEndDate <= endDateLine)
log.debug("condition matched");
returnValue = false;
alertMessage = "Start Date and End Date should not be between the existing date range";
alert(alertMessage);
break;
}
}
}
return returnValue;
}
} catch (error) {
log.error("error in validate line =", error);
}
};
return {
validateLine: validateLine,
};
});



Comments