Overview
Scheduled Scripts in NetSuite allow you to automate recurring tasks such as cleanup, reporting, and record updates. For beginners, this is one of the most powerful ways to reduce manual work.
In this guide, we’ll create a simple Scheduled Script that runs nightly to clean up stale or unnecessary data (e.g., closing old Sales Orders that are stuck in “Pending Fulfillment”).
Step-by-Step Guide: Creating and Deploying a Scheduled Script
1.Log in with Admin or Script Access Role
- Navigate to Customization > Scripting > Scripts > New.
2. Upload the Script File
- Under Script File, upload your `.js` file containing the Scheduled Script logic.
- Click Save and then Create Script Record.
3 . Create the Script Record
- Enter a Name (e.g., “Nightly Data Cleanup”).
- Save the record.
- Click Deploy
4. Deploy the Script
- On the Deployments subtab, click New Deployment.
- Select:
• Status: Released
• Log Level: Debug (recommended during testing)
• Scheduled Execution: Select Daily and choose 2:00 AM or another low-traffic time.
- Save the deployment
Sample Code: Nightly Cleanup of Old Sales Orders
/**
@NApiVersion 2.x
@NScriptType ScheduledScript
@NModuleScope SameAccount
*/ define(['N/record', 'N/search', 'N/log'], function(record, search, log) { function execute(context) { try { var soSearch = search.create({ type: search.Type.SALES_ORDER, filters: [ ['status', 'anyof', 'SalesOrd:B'], // Pending Fulfillment 'AND', ['datecreated', 'onorbefore', 'daysago90'] // older than 90 days ], columns: ['internalid'] });
soSearch.run().each(function(result) {
var soId = result.getValue('internalid');
var soRec = record.load({
type: record.Type.SALES_ORDER,
id: soId,
isDynamic: false
});
var lineCount = soRec.getLineCount({sublistId: 'item'});
for (var i = 0; i < lineCount; i++) {
// Set the "isclosed" field to true for each line
soRec.setSublistValue({
sublistId: 'item',
fieldId: 'isclosed',
line: i,
value: true
});
}
soRec.save();
log.debug('Closed Sales Order', 'ID: ' + soId);
return true;
});
} catch (e) {
log.error('Scheduled Script Error', e.message);
}
}
return { execute: execute };
});
Testing the Script
- Create a few old test Sales Orders (90+ days back) in Pending Fulfillment status.
- Edit Script Deployment Record & Deploy the script with a manual trigger first (Execute Now) to confirm it runs correctly.
- Check that the Sales Orders are updated to Closed.
- Review Script Execution Logs under Customization > Scripting > Script Execution Logs for debugging.
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.
Visit the SuiteCloud section under the New to NetSuite category to explore a range of beginner-friendly guides that walk you through essential accounting features in NetSuite.