Any reason why this script is not executed when th...
# suitescript
l
Any reason why this script is not executed when the context is CSV import even though I ticked the Run Server Script preference and the Deployment record includes CSV import context? Also, it works in the UI. (script in the reply section)
/**
* @NApiVersion 2.x
* @NScriptType UserEventScript
* @NModuleScope SameAccount
*/
define(['N/search'], function(search) {
function beforeSubmit(context) {
if (context.type === context.UserEventType.CREATE || context.type === context.UserEventType.EDIT) {
var newRecord = context.newRecord;
var uniqueId = newRecord.getValue({
fieldId: 'name'
});
var filters = [
search.createFilter({
name: 'name',
operator: <http://search.Operator.IS|search.Operator.IS>,
values: uniqueId
})
];
var existingId = search.create({
type: 'customrecord_abc_container',
filters: filters
}).run().getRange({
start: 0,
end: 1
});
// Check if any existing records were found
if (existingId.length > 0) {
throw new Error('The record already exists.');
}
}
}
return {
beforeSubmit: beforeSubmit
};
});
a
I'd insert a log before your context.type conditional and confirm it isn't actually executing
if you want to enforce uniqueness though, the standard way of doing that is to set the externalid
l
Thanks. I realized that it’s because of the throw error. I used the error module and it works now. The error is kind of weird. It’s not very straightforward to the user. I was expecting the it would show only the error message and not this: Error UniqueId {“type”“error.SuiteScriptError”,“name”“CUSTOM_ERROR”,“message”:“The record already exists.“,”id”null,“stack”[“createError(N/error)“,”beforeSubmit(/SuiteScripts/preventDuplicateRecords.js:34)“,”createError(N/error)“],“cause”{“name”“CUSTOM_ERROR”,“message”:“The recorc already exists.“},“notifyOff”false,“userFacing”true}
👍 1
If I use the External ID, that’s applicable only to records created by CSV, right? If I enforce it in the UI, per my testing, the record still gets created even if there is a duplicate because I can’t make beforeSubmit entry works. Works only if I use afterSubmit unless I’m missing something. We are also trying to avoid users accidentally not mapping the External ID in the CSV import which may bypass the process.
a
nope, external id is like internal id
once it has been set it won't allow you to create a new record with that ID
l
Yes, I understand that. But I can’t make it work using beforeSubmit in the UI. It only works for me in afterSubmit. In that case, the duplicate is already created but I get an error that the External ID already exists which did not prevent creation of the record. With CSV import, yes, it would prevent the creation if we always map the External ID. The issue with External ID is for records created in the UI. Unless I’m just missing something. Can’t really make it work using beforeSubmit
a
umm you wouldn't NEED any script to check for uniqueness, it would just fail to create so there'd be no need for afterSubmit, beforeSubmit - NS just won't allow it
sounds like maybe you have a weird use-case though, so maybe
externalId
isn't the solution here
l
We can’t set External ID in the UI. This is why we used script to set it automatically in the UI. With CSV import, yes, we don’t need the script.
👍 1
b
you can make the beforeSubmit make a companion custom record and sets its external id
if the create fails, there is a duplcate
l
@battk I’m sorry, can you elaborate on that? Am I going to create a custom record for the field that I want to enforce uniqueness? And use the External ID of that custom record in the beforeSubmit? Thanks.
b
set the name of the original custom record as the external id of the companion custom record
save the companion record
if it successfully saves, then its unique
if it doesnt, it isnt unique and you can throw an error
l
Thanks, @battk. I will try that.