I want to create user event script to avoid duplicate record creation. I have created script but its not working. Still permitting to create duplicate record means same field values.
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
*/
define(['N/record', 'N/email', 'N/url', 'N/runtime', 'N/search'],
function (record, email, url, runtime, search) {
function beforeSubmit(scriptContext) {
if (scriptContext.type === 'create' || scriptContext.type === 'edit') {
var rec = scriptContext.newRecord;
log.debug("rec", rec);
var recordId = record.id;
log.debug("recordId", recordId);
var category = rec.getValue({fieldId: 'custrecord_gbs_tren_cat'});
log.debug("category", category);
var exemptNexus = rec.getValue({fieldId: 'custrecord_gbs_tren_expt_nexus'});
log.debug("exemptNexus", exemptNexus);
//throw error.create("You cannot create duplicate record");
var existing_record = false;
var customrecord_gbs_tax_rule_expt_nexusSearchObj = search.create({
type : "customrecord_gbs_tax_rule_expt_nexus",
filters:
[
["custrecord_gbs_tren_cat", "anyof", category],
"AND",
["custrecord_gbs_tren_expt_nexus", "anyof", exemptNexus]
],
columns:
[
search.createColumn({name: "custrecord_gbs_tren_cat", label: "Category"}),
search.createColumn({name: "custrecord_gbs_tren_expt_nexus", label: "Exempt Nexus"})
]
});
customrecord_gbs_tax_rule_expt_nexusSearchObj.runPaged().count;
//log.debug("expensereportSearchObj result count",searchResultCount);
// alert('Expenses Count: '+searchResultCount);
customrecord_gbs_tax_rule_expt_nexusSearchObj.run().each(function () {
// .run().each has a limit of 4,000 results
existing_record = true;
return false;
});
}
}
return {
beforeSubmit: beforeSubmit
}
});
If you want to user event script to show error to user then use N/error module and do like this in script:-
I hope it will work.
If it is relevant please mark as green for others help as well.
Thanks,