Scenario:
A client script to validate the entry of the new SKU/item in each line. When the user adds a line item to the sales order, if it already exists, the script will not allow the user to add it again. An alert message will notify the user: “This SKU/item has already been added once. Please try adding a different Item”. This alert message will be applicable during the creation of a new sales order record, and in edit mode.
/**
* @NApiVersion 2.1
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
define(['N/record', 'N/search'],
/**
* @param{record} record
* @param{search} search
*/
function (record, search) {
"use strict"
/**
* variable to store restricted item details
*/
const PENDING_APPROVAL = 'A';
const PENDING_FULFILLMENT = 'B';
const KIT_ITEM = "Kit"
let CONTEXT_MODE;
let RECORD_TYPE;
let OLD_ITEM;
let FIRST_OCCURENCE = '';
/**
* @description Check whether the given parameter argument has value on it or is it empty.
* ie, To check whether a value exists in parameter
* @param {*} parameter parameter which contains/references some values
* @param {*} parameterName name of the parameter, not mandatory
* @returns {Boolean} true if there exist a value else false
*/
function checkForParameter(parameter, parameterName) { // To check whether a value exists in parameter
if (parameter != "" && parameter !== null && parameter !== undefined && parameter !== false && parameter !== "null" && parameter !== "undefined" && parameter !== " " && parameter !== 'false') {
return true;
} else {
if (parameterName)
log.debug('Empty Value found', 'Empty Value for parameter ' + parameterName);
return false;
}
}
/**
* 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) {
CONTEXT_MODE = scriptContext.mode;
let currentRec = scriptContext.currentRecord;
RECORD_TYPE = currentRec.type;
// STATUS = currentRec.getValue({
// fieldId: 'orderstatus'
// });
}
/**
* Validation function to be executed when field is changed.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
* @param {string} scriptContext.fieldId - Field name
* @param {number} scriptContext.lineNum - Line number. Will be undefined if not a sublist or matrix field
* @param {number} scriptContext.columnNum - Line number. Will be undefined if not a matrix field
*
* @returns {boolean} Return true if field is valid
*
* @since 2015.2
*/
function validateField(scriptContext) {
let currentRecord = scriptContext.currentRecord;
let fieldId = scriptContext.fieldId;
let sublistId = scriptContext.sublistId;
if (sublistId === 'item' && fieldId == 'item' && RECORD_TYPE == 'salesorder') {
let newItem = currentRecord.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'item'
});
if (!checkForParameter(newItem)) {
alert("Please choose an item to add");
// return false;
} else {
let itemTypesearch = search.lookupFields({
type: search.Type.ITEM,
id: newItem,
columns: ['type']
});
let itemType = itemTypesearch.type[0].value;
if (itemType != KIT_ITEM) {
FIRST_OCCURENCE = currentRecord.findSublistLineWithValue({
sublistId: 'item',
fieldId: 'item',
value: newItem
});
let currentLineIndex = currentRecord.getCurrentSublistIndex({
sublistId: 'item'
});
let lineUniqueId = currentRecord.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'lineuniquekey',
});
return true;
} else {
return true;
}
}
} else {
return true;
}
}
/**
* Validation function to be executed when sublist line is committed.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
*
* @returns {boolean} Return true if sublist line is valid
*
* @since 2015.2
*/
function validateLine(scriptContext) {
try {
let currentRecord = scriptContext.currentRecord;
let sublistId = scriptContext.sublistId;
if (sublistId === 'item' && RECORD_TYPE == 'salesorder') {
let newItem = currentRecord.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'item'
});
if (!checkForParameter(newItem)) {
alert("Please choose an item to add");
return false;
} else {
let itemTypesearch = search.lookupFields({
type: search.Type.ITEM,
id: newItem,
columns: ['type']
});
let itemType = itemTypesearch.type[0].value;
if (itemType != KIT_ITEM) {
let currentLineIndex = currentRecord.getCurrentSublistIndex({
sublistId: 'item'
});
let lineUniqueId = currentRecord.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'lineuniquekey',
});
if (!checkForParameter(lineUniqueId)) {
if ((FIRST_OCCURENCE != currentLineIndex) && (FIRST_OCCURENCE != -1)) {
alert('This SKU/item has already been added once. Please try adding a different Item');
return false;
} else {
return true;
}
} else {
if ((OLD_ITEM == newItem)) {
return true;
} else {
if ((FIRST_OCCURENCE != currentLineIndex) && (FIRST_OCCURENCE != -1)) {
alert('This SKU/item has already been added once. Please try adding a different Item');
return false;
}
else {
return true;
}
}
}
} else {
return true;
}
}
}
} catch (err) {
console.error("error@validateLine", err)
}
}
/**
* Function to be executed after line is selected.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
*
* @since 2015.2
*/
function lineInit(scriptContext) {
let currentRecord = scriptContext.currentRecord;
let sublistId = scriptContext.sublistId;
if (sublistId == 'item') {
OLD_ITEM = currentRecord.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'item'
});
}
}
/**
* Validation function to be executed when sublist line is inserted.
*
* @param {Object} scriptContext
* @param {Record} scriptContext.currentRecord - Current form record
* @param {string} scriptContext.sublistId - Sublist name
*
* @returns {boolean} Return true if sublist line is valid
*
* @since 2015.2
*/
function validateInsert(scriptContext) {
let currentRecord = scriptContext.currentRecord;
let sublistId = scriptContext.sublistId;
let newInsertedItem = currentRecord.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'item'
});
let lineUniqueId = currentRecord.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'lineuniquekey',
});
let currentLineIndex = currentRecord.getCurrentSublistIndex({
sublistId: 'item'
});
if ((OLD_ITEM == newInsertedItem)) {
return true;
} else {
if ((FIRST_OCCURENCE != currentLineIndex) && (FIRST_OCCURENCE != -1)) {
// alert('This SKU/item has already been added once. Please try adding a different Item');
return false;
}
else {
return true;
}
}
}
return {
pageInit: pageInit,
validateField: validateField,
validateLine: validateLine,
lineInit: lineInit,
validateInsert: validateInsert,
};
});