Hi everyone, I'm trying to prevent a vendor bill f...
# suitescript
a
Hi everyone, I'm trying to prevent a vendor bill from being saved when the same vendor has a field value X registered in another previous invoice (The key would be (vendor + X value). I'm doing this through a userEvent in the beforeSubmit, however, I see that even though it reaches the condition, it doesn't prevent it from being saved and it continues to be saved, nor does it show me the alert message. I know it reaches the condition because it shows me a previous log message that I put. Thank you.
b
what does the code look like
a
/**
@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 }; });
c
@Alexander Cuadros beforeSubmit is a server script, so `message.show()`won't function there. Also, I'm not sure what
context.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.
馃憤 1