When a script runs on the wrong record type, it can create errors, update the wrong data, or make troubleshooting harder. Even when a script is deployed carefully, it is a good practice to add checks in the script logic to confirm that it is running in the expected context.
This helps make SuiteScript behavior safer and easier to support.
Scenario
An admin or developer creates a script for one record type.
Examples:
- Customer
- Vendor
- Sales Order
- Purchase Order
- Custom Record
The script is intended to run only for that record type, but due to deployment changes, testing, or reuse of script logic, it may run somewhere unexpected.
If the script assumes the wrong fields or record structure, it may fail or produce incorrect results.
Solution
Add a record type check before running the main script logic.
The script should confirm that the current record type matches the expected record type. If it does not match, the script should stop processing.
This is sometimes called a script guard or safety check.
What to Check
Before the script continues, confirm:
- the expected record type
- the deployment record type
- whether the script should run on create, edit, or delete
- whether the required fields exist on the record
- whether the script is being reused across multiple records
These checks help prevent the script from applying logic in the wrong place.
Sample Script Guard
The sample below shows a simple User Event Script that checks the record type before continuing.
/**
* @NApiVersion 2 .1
* @NScriptType UserEventScript
*/
define(['N/log'], (log) => {
const EXPECTED_RECORD_TYPE = 'customrecord_request_tracker';
function beforeSubmit(context) {
const rec = context.newRecord;
if (rec.type !== EXPECTED_RECORD_TYPE) {
log.audit({
title: 'Script Skipped',
details: `Expected ${EXPECTED_RECORD_TYPE}, but received ${rec.type}`
});
return;
}
// Add record-specific logic here.
log.audit({
title: 'Record Type Confirmed',
details: `Script is running on ${rec.type}`
});
}
return {
beforeSubmit
};
});
How It Works
The script checks the current record type using:
rec.type
If the record type does not match the expected value, the script logs a message and stops by using:
return;
If the record type matches, the script continues with the intended logic.
Notes
- Deploy the script on the correct record type.
- Add a record type check when the script should only run in one context.
- Use clear log messages when skipping execution.
- Confirm field IDs exist on the target record type.
- Test with create, edit, and delete scenarios if applicable.
- Review deployments after copying or reusing scripts.
- Use sandbox before production deployment.
Disclaimer :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.
Share your insights and experiences in the NetSuite Admin Corner.