Maintaining complete customer data is critical for downstream processes such as invoicing, communications, and integrations. As an administrator, you may want visibility when a Customer record is saved without essential information—such as an email address or at least one physical address.
This tip demonstrates how to use a User Event Script to automatically detect missing customer details and log an Emergency message when required fields are incomplete.
Use Case
- Ensure customer records always include an email address and at least one address.
- Flag incomplete data immediately after record save.
- Surface critical data issues clearly in script execution logs for production monitoring.
Sample Script (afterSubmit – Customer)
/**
* @NApiVersion 2 .x
* @NScriptType UserEventScript
*/
define(['N/log'], function(log) {
/**
* afterSubmit event handler.
* Checks if the customer record is missing email and/or address and logs an emergency if so.
*/
function afterSubmit(context) {
var newRecord = context.newRecord;
// Get the value of the email field from the customer record
var email = newRecord.getValue({ fieldId: 'email' });
// Check the number of addresses in the addressbook sublist
var addressCount = newRecord.getLineCount({ sublistId: 'addressbook' });
// Collect which fields are missing
var missingFields = [];
// Check if email is missing
if (!email) {
missingFields.push('email');
}
// Check if at least one address is present
if (!addressCount || addressCount === 0) {
missingFields.push('address');
}
// Log an emergency if either field is missing
if (missingFields.length > 0) {
log.emergency({
title: 'Customer Data Missing',
details: 'A Customer record was saved missing the following: ' +
missingFields.join(' and ') +
'. Record ID: ' + newRecord.id
});
}
}
return {
afterSubmit: afterSubmit
};
});
Using an afterSubmit User Event Script on the Customer record, the script:
- Retrieves the customer’s email value.
- Checks the number of entries in the Address Book sublist.
- Identifies which required fields are missing.
- Triggers an Emergency log if either the email or address (or both) are missing.
Emergency logs always appear in the execution logs, making them ideal for critical data validation in production environments.
The sample code described herein is provided on an "as is" basis, without warranty of any kind, to the fullest extent permitted by law. Oracle + NetSuite Inc. does not warrant or guarantee the individual success developers may have in implementing the sample code on their development platforms or in using their own Web server configurations.
Oracle + NetSuite Inc. does not warrant, guarantee or make any representations regarding the use, results of use, accuracy, timeliness or completeness of any data or information relating to the sample code. Oracle + NetSuite Inc. disclaims all warranties, express or implied, and in particular, disclaims all warranties of merchantability, fitness for a particular purpose, and warranties related to the code, or any service or software related thereto.
Oracle + NetSuite Inc. shall not be liable for any direct, indirect or consequential damages or costs of any type arising out of any action taken by you or others related to the sample code.
To know more about deploying SuiteScript, check these New to NetSuite Articles.
Do you have another way on how to automate your transactions using SuiteScript? Feel free to share them here!