As an Administrator, there are times when you need to quickly review or extract specific transactional data without going through multiple saved searches or reports. If you're looking to programmatically retrieve all invoices from a specific date onward—like the beginning of 2025—SuiteScript can help streamline this task.
Below is a simple SuiteScript 2.x example that creates a search for invoices dated January 1, 2025, and later. It then logs key details such as the Invoice Number, Customer, Total Amount, and Transaction Date.
require(['N/search'], function(search) {
var invSearch = search.create({
type: search.Type.INVOICE,
filters: [
['mainline', search.Operator.IS, 'T'],
'and',
['trandate', search.Operator.ONORAFTER, '1/1/2025']
],
columns: [
{ name: 'tranid' },
{ name: 'entity' },
{ name: 'total' },
{ name: 'trandate' }
],
title: 'Invoice'
});
var resultSet = invSearch.run();
resultSet.each(function(result) {
console.log('-----------');
console.log('Invoice ID : ' + result.getValue({ name: 'tranid' }));
console.log('Customer : ' + result.getText({ name: 'entity' }));
console.log('Total : ' + result.getValue({ name: 'total' }));
console.log('Date : ' + result.getValue({ name: 'trandate' }));
console.log('-----------');
return true;
});
});
Code Breakdown
require(['N/search'], function(search) {
- This imports the search module, which allows you to create and run saved-search-like queries using SuiteScript.
var invSearch = search.create({
type: search.Type.INVOICE,
filters: [
['mainline', search.Operator.IS, 'T'],
'and',
['trandate', search.Operator.ONORAFTER, '1/1/2025']
],
- type: We're targeting Invoices.
- filters:
- mainline = T ensures only the main transaction line is included (avoids line-level details).
- trandate on or after 1/1/2025 filters invoices to only those dated Jan 1, 2025 or later.
columns: [
{ name: 'tranid' },
{ name: 'entity' },
{ name: 'total' },
{ name: 'trandate' }
],
These define the data we want to retrieve:
- tranid: Invoice number
- entity: Customer
- total: Total amount
- trandate: Transaction date
var resultSet = invSearch.run();
- Executes the search and returns a resultSet object that we can loop through.
resultSet.each(function(result) {
console.log('-----------');
console.log('Invoice ID : ' + result.getValue({ name: 'tranid' }));
console.log('Customer : ' + result.getText({ name: 'entity' }));
console.log('Total : ' + result.getValue({ name: 'total' }));
console.log('Date : ' + result.getValue({ name: 'trandate' }));
console.log('-----------');
return true;
});
- resultSet.each() loops through each invoice.
- Inside the loop:
- getValue() fetches the raw value (e.g., internal ID, date, amount).
- getText() is used for fields that reference other records (like the customer name).
- return true; ensures the loop continues to the next result.
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.
Tip: You can modify the filters or add more columns (like status or currency) depending on your reporting needs.
To know more about deploying SuiteScript, check these New to NetSuite Articles.
Do you have another way on how to create saved searches in SuiteScript? Feel free to share them here!