In many businesses, invoices stuck in the Pending Approval status can cause delays in financial reporting and process flows. If your team has a policy to reject certain invoices automatically—for example, those not approved by the end of the day—you can automate this using SuiteScript.
This tip shows you how to create a Scheduled Script that automatically updates the approval status of invoices from Pending Approval (1) to Rejected (3) based on a specific condition—in this case if the invoice date is today.
Script Summary:This script searches for invoices dated today with "Pending Approval" status and updates them to "Rejected"
/**
* @NApiVersion 2.x
* @NScriptType ScheduledScript
* @NModuleScope SameAccount
*/
define(['N/search','N/record'],
function(search,record) {
/**
* Definition of the Scheduled script trigger point.
*
* @param {Object} scriptContext
* @param {string} scriptContext.type - The context in which the script is executed. It is one of the values from the scriptContext.InvocationType enum.
*/
function execute(scriptContext) {
var myFilter = search.create({
type:search.Type.INVOICE,
filters: [{
name: 'trandate',
operator: search.Operator.WITHIN,
values:'today'
},{
name: 'approvalstatus',
operator: search.Operator.ANYOF,
values: '1' // Pending Approval
}]
}).run().each(function(result){
record.submitFields({
type: record.Type.INVOICE,
id:result.id,
values: {
approvalstatus:'3' // Rejected
}
})
return true
})
log.debug('done')
}
return {
execute: execute
};
});
Code Breakdown
var myFilter = search.create({
type:search.Type.INVOICE,
filters: [{
name: 'trandate',
operator: search.Operator.WITHIN,
values:'today'
},{
name: 'approvalstatus',
operator: search.Operator.ANYOF,
values: '1' // Pending Approval
}]
})
type: search.Type.INVOICE: Looks only at Invoice records.filters: Applies two conditions:trandate WITHIN today: Only considers invoices dated today.approvalstatus ANYOF 1: Only includes invoices that are Pending Approval (1 is the internal ID for that status).
.run().each(function(result) {
.run().each(...): Executes the search and loops through each result using a callback function.
record.submitFields({
type: record.Type.INVOICE,
id: result.id,
values: {
approvalstatus: '3' // Rejected
}
});
submitFields: A lightweight way to update specific fields on a record.approvalstatus: '3': Changes the approval status to Rejected (3 is the internal ID for Rejected).
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 automate your Invoices using SuiteScript? Feel free to share them here!