Jira Code: Task in ESW
We are able to save the record even though the duplicate customers/vendors are there. It will show only warning message for the duplicate and allow to create the record. This script will block the duplicate customer/vendor creation.
The script will search the duplicate customer/vendor using the search function in script and throw an error before saving the record.
Code
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/
define(['N/search', 'N/record', 'N/error'],
function(search, record, error) {
var mainRoot = {
beforeSubmit: function(scriptContext) {
log.debug("scriptContext.type", scriptContext.type);
log.debug("scriptContext.newRecord.type", scriptContext.newRecord);
var newRecord = scriptContext.newRecord;
var isCOMPANY = newRecord.getValue({
fieldId: 'isperson'
});
isCOMPANY = ((isCOMPANY == 'f' || isCOMPANY == 'F' || isCOMPANY == 'false' || isCOMPANY == false) ? (true) : (false));
if (isCOMPANY)
if (mainRoot.isCompanyExists(newRecord))
showError(scriptContext.newRecord.type);
/* else
return true;
else
return true;*/
},
isCompanyExists: function(currentRecord) {
var companyName = currentRecord.getValue({
fieldId: 'companyname'
});
var subsidiary = currentRecord.getValue({
fieldId: 'subsidiary'
});
var numLines = currentRecord.getLineCount({
sublistId: 'addressbook'
});
log.debug("numLines", numLines);
for(var i=0;i<numLines;i++)
{
var defBilling = currentRecord.getSublistValue({
sublistId: 'addressbook',
fieldId: 'defaultbilling',
line: i
});
log.debug("defBilling", defBilling);
if(defBilling==true)
{
var myaddressSubrecord = currentRecord.getSublistSubrecord({
sublistId : 'addressbook',
fieldId : 'addressbookaddress',
line:i
});
var zip=myaddressSubrecord.getValue({
fieldId : 'zip'
});
log.debug("zip", zip);
}
}
var filt = [];
filt = (currentRecord.type == "vendor") ?
[
["isperson", "is", "F"],
"AND", ["entityid", "is", companyName],
"AND",
["zipcode","is",zip]
] :
[
["isperson", "is", "F"],
"AND", ["companyname", "is", companyName],
"AND", ["subsidiary", "anyof", subsidiary],
"AND",
["zipcode","is",zip]
];
var customerSearchObj = search.create({
type: currentRecord.type,
filters: filt,
columns: [
search.createColumn({
name: "internalid",
sort: search.Sort.ASC,
label: "Internal ID"
}),
search.createColumn({
name: "isperson",
label: "Is Individual"
}),
search.createColumn({
name: "companyname",
label: "Company Name"
}),
search.createColumn({
name: "subsidiary",
label: "Primary Subsidiary"
}),
search.createColumn({
name: "subsidiarynohierarchy",
label: "Primary Subsidiary (no hierarchy)"
})
]
}).run().getRange({
start: 0,
end: 2
});
if (customerSearchObj.length > 0)
return true;
else
return false;
}
};
function showError(recType) {
var errorName = null;
var errorMsg = null;
if (recType == "vendor") {
errorName = "DUPLICATE_VENDOR";
errorMsg = 'Vendor Already Exits for the same subsidiary and company';
} else if (recType == "customer") {
errorName = "DUPLICATE_CUSTOMER";
errorMsg = 'Customer Already Exits for the same subsidiary and company';
}
var errObj = error.create({
name: errorName,
message: errorMsg,
notifyOff: false
});
log.error('Error: ' + errObj.name, errObj.message);
throw errObj;
}
/* for (var key in mainRoot) {
if (typeof mainRoot[key] === 'function') {
mainRoot[key] = trycatch(mainRoot[key], key);
}
}
function trycatch(myfunction, key) {
return function() {
try {
return myfunction.apply(this, arguments);
} catch (e) {
log.debug("Error in " + key, e);
return false;
}
}
};*/
return mainRoot;
});