Keeping track of overdue Invoices is essential for maintaining healthy cash flow and ensuring timely collections. As an Administrator, you can automate the monitoring of open invoices that are already past their due date using a SuiteScript.
This article walks you through how to search for open invoices overdue as of today and log relevant Customer and Invoice details for follow-up or reporting purposes.
This Scheduled Script:
- Gets today’s date.
- Formats it according to NetSuite search requirements (M/d/yy).
- Searches for Invoices that:
- Have status Open (CustInvc:A)
- Have a due date earlier than today
- Loads the related Customer record to retrieve the company name.
- Logs the Invoice details for audit or monitoring.
Example SuiteScript
/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
*/
define(['N/record', 'N/search', 'N/log'], function(record, search, log) {
/**
* Formats a JavaScript Date object as 'M/d/yy' for NetSuite search filters
* @param {Date} date
* @returns {string}
*/
function formatDateForNetSuite(date) {
var month = date.getMonth() + 1; // getMonth() is zero-based
var day = date.getDate();
var year = date.getFullYear();
return month + '/' + day + '/' + year;
}
/**
* Main scheduled script function
*/
function execute(context) {
// Get today's date in NetSuite's required format
var today = new Date();
var todayStr = formatDateForNetSuite(today);
// Create a search for all open (unpaid) invoices overdue as of today
var invoiceSearch = search.create({
type: record.Type.INVOICE,
filters: [
['status', 'anyof', 'CustInvc:A'], // 'A' = Open Invoice
'AND',
['duedate', 'before', todayStr] // Due before today (overdue)
],
columns: [
'internalid', // Invoice internal ID
'entity', // Customer ID
'duedate', // Due Date
'tranid' // Invoice Number
]
});
// Run the search and process each result independently
invoiceSearch.run().each(function(result) {
var invoiceId = result.getValue({ name: 'internalid' });
var customerId = result.getValue({ name: 'entity' });
var dueDate = result.getValue({ name: 'duedate' });
var invoiceNumber = result.getValue({ name: 'tranid' });
// If customerId is missing, log and skip this invoice
if (!customerId) {
log.debug({
title: 'Invoice skipped due to missing customer',
details: 'Invoice internalid: ' + invoiceId
});
return true; // Continue to next result
}
try {
// Load customer record to get company name
var customerRec = record.load({
type: record.Type.CUSTOMER,
id: customerId
});
var companyName = customerRec.getValue({ fieldId: 'companyname' });
// Log invoice details with customer name
log.audit({
title: 'Overdue Invoice: #' + invoiceNumber,
details: 'Customer: ' + companyName +
', Due Date: ' + dueDate +
', Invoice ID: ' + invoiceId
});
} catch (e) {
// Log any errors encountered during customer record loading
log.error({
title: 'Error loading customer ID ' + customerId,
details: e.message || e.toString()
});
}
return true; // Continue processing invoices
});
}
return {
execute: execute
};
});
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!