Requirement
The requirement is to either allow or disallow items to be selected on a sales order based on the user’s certifications and the items being chosen.
If a user does not meet the criteria for selling an item when it is added to the Sales Order through the UI, a popup needs to notify the user, and the item should not be allowed to be added.
Solution
A client script can be deployed on the sales order record.
This script is triggered when a user attempts to add an item to a sales order. The script will retrieve the current user’s certifications from the employment record. In the employee record, there are four certification checkboxes. The item will be categorized in accordance with the certification. We will check that the item has the same classification before allowing the user to add it to the sales order.
Suppose the item’s categorization does not match the employee’s certification. In that case, we will not allow the user to add the item to the sales order, we will show a popup with this message, and we will remove the item from the sales order.
/**
* @NApiVersion 2.x
* @NScriptType ClientScript
* @NModuleScope SameAccount
*/
define(['N/currentRecord', 'N/record', 'N/search'],
/**
* @param{currentRecord} currentRecord
* @param{record} record
* @param{search} search
*/
function (currentRecord, record, search) {
/**
* 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 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
*
* @since 2015.2
*/
function fieldChanged(scriptContext) {
try {
var currentRecord = scriptContext.currentRecord;
var recId = currentRecord.id
if ((scriptContext.sublistId === 'item') && (scriptContext.fieldId === 'item')) {
if (recId == "") {
var checkAlert = true
var salesPerson = currentRecord.getValue({
fieldId: "custbody45"
})
var itemValue = currentRecord.getCurrentSublistValue({
sublistId: 'item',
fieldId: 'item'
});
if (itemValue !== "") {
var emplCertifcateSearch = employeeCertifcateSearch(salesPerson)
var itemCertificateSearch = itemRecCertificateSearch(itemValue)
if ((emplCertifcateSearch.length > 0) && (itemCertificateSearch.length > 0)) {
if (emplCertifcateSearch.indexOf(itemCertificateSearch[0].getText({
name: "custitem_jj_certificate_ahap_409",
label: "Certification"
})) !== -1) {
} else {
alert("Sales Associate does not have enough certification to add this item!")
checkAlert = false
}
} else {
alert("Sales Associate does not have enough certification to add this item!")
checkAlert = false
}
if (checkAlert == false) {
currentRecord.setCurrentSublistValue({
sublistId: "item",
fieldId: "item",
value: ""
})
console.log("set quantity empty")
currentRecord.cancelLine({
sublistId: 'item'
});
}
}
}
}
} catch (e) {
console.log("error@ItemMangementFieldChange", e)
}
}
function employeeCertifcateSearch(intId) {
try {
var employeeSearchObj = search.create({
type: "employee",
filters:
[
["internalid", "anyof", intId],
"AND",
[["custentity_aha_core", "is", "T"], "OR", ["custentity_aha_pro", "is", "T"], "OR", ["custentity_aha_riggs", "is", "T"], "OR", ["custentity_aha_gagg", "is", "T"]]
],
columns:
[
search.createColumn({name: "custentity_aha_core", label: "Core"}),
search.createColumn({name: "custentity_aha_pro", label: "Luxury"}),
search.createColumn({name: "custentity_aha_riggs", label: "RIGGS"}),
search.createColumn({name: "custentity_aha_gagg", label: "GAGG"})
]
});
var searchResult = employeeSearchObj.run().getRange({
start: 0,
end: 1
})
var empCertficateArray = []
if (searchResult.length > 0) {
for (var colummNo = 0; colummNo < searchResult[0].columns.length; colummNo++) {
if (searchResult[0].getValue(searchResult[0].columns[colummNo]) == true) {
empCertficateArray.push(searchResult[0].columns[colummNo].label)
}
}
return empCertficateArray
} else {
return []
}
} catch (e) {
console.log("error@itemManagement", e)
}
}
function itemRecCertificateSearch(itemId) {
try {
var inventoryitemSearchObj = search.create({
type: "inventoryitem",
filters:
[
["type", "anyof", "InvtPart"],
"AND",
["internalid", "anyof", itemId],
"AND",
["custitem_jj_certificate_ahap_409", "anyof", "1", "2", "3", "4"]
],
columns:
[
search.createColumn({name: "internalid", label: "Internal ID"}),
search.createColumn({name: "custitem_jj_certificate_ahap_409", label: "Certification"})
]
});
var searchResult = inventoryitemSearchObj.run().getRange({
start: 0,
end: 1
})
if (searchResult.length > 0) {
return searchResult
} else {
return []
}
} catch (e) {
console.log("error@itemRecCertificateSearch", e)
}
}
return {
pageInit: pageInit,
fieldChanged: fieldChanged,
};
});