/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
/*******************************************************************
* ClientScript
****************************************************************
*
* Date: 11/01/2023
*
* Author: Jobin and Jismi IT Services LLP
*
* REVISION HISTORY
*
* Revision 1.0
*
* Description: Script to check whether the purchase order item lines contains duplicates
*
***************************************************************/
define(['N/currentRecord', 'N/search', 'N/ui/dialog', 'N/ui/message','N/url'],
/**
* @param{currentRecord} currentRecord
* @param{search} search
* @param{dialog} dialog
* @param{message} message
* @param{message} url
*/
function(currentRecord, search, dialog, message,url) {
/**
* Function to check whether the elements in an array are same or not
* @param itemArray
* *@param arrLength
*/
function compareItemLoc(itemArray, arrLength) {
try {
// initialize ifPresent as false
var ifPresent = false;
// ArrayList to store the output
var al = new Array();
for (var i = 0; i < arrLength - 1; i++) {
for (var j = i + 1; j < arrLength; j++) {
if (itemArray[i] == itemArray[j]) {
if (al.includes(itemArray[i])) {
break;
}
else {
al.push(itemArray[i]);
ifPresent = true;
}
}
}
}
if (ifPresent == true) {
return al;
}
}catch (e) {
}
}
/**
* Validation function to be executed when record is saved.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @returns {boolean} Return true if record is valid
*
* @since 2015.2
*/
function saveRecord(scriptContext) {
try {
// get the current PO record
var poRec=scriptContext.currentRecord;
var poItemCount=poRec.getLineCount({sublistId:'item'})
console.log("poItemCount",poItemCount)
// Loop through the item line
var i;
var itemArray=[];
var poItemId
for (i=0;i<poItemCount;i++){
poItemId = poRec.getSublistValue({sublistId: 'item', fieldId: 'item', line: i})
itemArray.push(poItemId)
}
var arrLength=itemArray.length
// function to check whether the array contains duplicates
var duplicates=compareItemLoc(itemArray,arrLength)
// create alert message
if (duplicates){
var content2 = "Please remove duplicate Items in this purchase order!! " ;
var options = {
title: "Purchase order item line Suggestions",
message: content2
};
dialog.alert(options);
return false
}
return true
}catch (e) {
console.log("error @ saveRecord",e)
}
}
return {
saveRecord: saveRecord
};
});