Requirement
We need the prevention of double-entry of the same Customer PO. We can show an alert message while trying to save the record. This alert also prevents the user from saving the sales order record. To check the double entry, we will check if there are any already existing sales order that has the same PO# number. If so, then we will restrict the SO creation.
We need to consider the Customer and PO combination and make sure that it is different.
Solution
/**
* @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 checkDuplicatePO(poNum,customer){
var salesorderSearchObj = search.create({
type: "salesorder",
filters:
[
["type","anyof","SalesOrd"],
"AND",
["otherrefnum","equalto",poNum],
"AND",
["customer.internalid","anyof",customer],
"AND",
["mainline","is","T"]
],
columns:
[
search.createColumn({name: "internalid", label: "Internal ID"}),
search.createColumn({name: "otherrefnum", label: "PO/Check Number"})
]
});
var searchRes = salesorderSearchObj.runPaged().count;
console.log('Counnt in search',searchRes)
return searchRes;
}
/**
* 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{
var currentRec = scriptContext.currentRecord;
console.log('Current.Record',currentRec)
var poNum = currentRec.getValue({
fieldId: 'otherrefnum'
});
var customer = currentRec.getValue({
fieldId: 'entity'
});
console.log('custN',customer)
console.log('PO Value',poNum)
if((poNum!='')&&(customer!='')){
var searchResultCount=checkDuplicatePO(poNum,customer);
console.log('Count',searchResultCount)
if(searchResultCount>0){
alert('You have entered a duplicate PO for this customer')
return false
}
return true;
}
}
catch (e) {
console.log('Error@SveRecord',e)
}
}
return {
saveRecord: saveRecord
};
});