Alexander Cuadros
03/21/2024, 8:33 PMbattk
03/21/2024, 9:06 PMAlexander Cuadros
03/21/2024, 9:59 PM/**
@NApiVersion 2.1
@NScriptType UserEventScript
@NModuleScope SameAccount
*/
define(['N/search', 'N/record', 'N/ui/message', 'N/log', 'N/task', 'N/format'],
function (search, record, message, log, task, format) {
function beforeSubmit(context) {
log.audit("alex","alex");
if (context.type !== context.UserEventType.CREATE && context.type !== context.UserEventType.EDIT)
return;
var newRecord = context.newRecord;
var vendorId = newRecord.getValue({
fieldId: 'entity'
});
log.audit("vendorId",vendorId);
var custbodyEgiApNcf = newRecord.getValue({
fieldId: 'custbody_egi_ap_ncf'
});
log.audit("custbodyEgiApNcf",custbodyEgiApNcf);
if (vendorId && custbodyEgiApNcf) {
// Buscar registros existentes con la misma combinaci贸n de vendor y custbody_egi_ap_ncf
var existingFilters = [
['entity', 'is', vendorId],
'AND',
['custbody_egi_ap_ncf', 'is', custbodyEgiApNcf]
];
if (context.type === context.UserEventType.EDIT) {
// Excluir el registro actual en caso de edici贸n
existingFilters.push('AND');
existingFilters.push(['internalid', 'noneof', context.newRecord.id]);
}
var existingRecordsSearch = search.create({
type: search.Type.VENDOR_BILL,
filters: existingFilters
});
log.audit("existingRecordsSearch",existingRecordsSearch);
var searchResults = existingRecordsSearch.run().getRange({ start: 0, end: 1 });
log.audit("searchResults",searchResults);
if (searchResults && searchResults.length > 0) {
log.audit("encontr贸","encontro");
// Mostrar mensaje de alerta y cancelar el guardado
var messageText = 'No se puede guardar el registro. Ya existe una factura con el mismo Proveedor y NCF.';
var messageInstance = message.create({
title: 'Validaci贸n de Duplicados',
message: messageText,
type: message.Type.ERROR
});
messageInstance.show();
// Cancelar el guardado del registro
context.cancel = true;
}
}
}
return {
beforeSubmit: beforeSubmit
};
});Clay Roper
03/21/2024, 10:16 PMcontext.cancel
is -- I've never seen that on any script context.
If you want to stick with a beforeSubmit instead of a saveRecord, you'll want to throw an error to prevent saving.
If you want to switch to a saveRecord to perform this in the client, you would return false when a duplicate exists and true otherwise.