Introduction
NetSuite provides a variety of scheduling options for scripts, such as weekly or monthly runs, but it doesn’t offer a direct option to run a script specifically on the last day of the month. However, many business processes require end-of-month tasks, such as closing financial reports, updating records, or triggering specific workflows.
To address this limitation, you can include a simple date-checking logic in your Scheduled Script that ensures it only runs on the last day of each month. This article explains the approach and provides the code you can integrate into your NetSuite scripts to achieve this.
Purpose of the Script
Running a script on the last day of the month is crucial for various end-of-month operations, such as:
- Financial close processes
- Sending automated monthly reports
- Performing reconciliations
- Processing specific transactions
Using the SuiteScript API, you can check if the current day is the last day of the month, ensuring the script logic only executes when it is time to perform these tasks.
How It Works
Key Concept
The Date API in JavaScript provides functionality to manage and manipulate dates easily. Using this, we can determine:
- Today’s Date: The current day of the month.
- Last Day of the Month: By setting the date of a
new Date()object to the 0th day of the next month, JavaScript returns the last day of the current month.
Once this check is made, the script will only proceed if the current day matches the last day of the month.
Code Implementation
Here’s the logic in the script:
let today = new Date();
let todays_date = today.getDate(); // Get the current day
let todays_month = today.getMonth() + 1; // Get the current month (Note: JavaScript months are 0-indexed)
let todays_year = today.getFullYear(); // Get the current year
// Determine the last day of the current month
let last = new Date(todays_year, todays_month, 0); // The last day of the current month is the 0th day of next month
let last_date = last.getDate(); // Get the numeric last date of the month
// If today is the last day of the month, execute the scheduled script logic
if (todays_date === last_date) {
log.debug("Today is the last day of the month.", `Running script for end-of-month tasks.`);
// Place the end-of-month script logic here
} else {
log.debug("Today is not the last day of the month.", `Script will not run.`);
}
Explanation of the Code
- Getting the Current Date:
new Date()gives the current date and time.today.getDate()returns the day of the month.today.getMonth() + 1gives the current month (adjusting for JavaScript’s 0-indexed months).today.getFullYear()gives the current year.
- Calculating the Last Day of the Month:
new Date(todays_year, todays_month, 0)creates a new date object for the last day of the current month. By setting the day to0in the next month, it returns the last day of the current month.last.getDate()retrieves the actual numeric day of the month (e.g., 30 for June, 31 for July).
- Checking if Today is the Last Day:
- The script compares
todays_datetolast_date. If they are equal, it means today is the last day of the month, and the scheduled script’s logic is executed.
- Logging:
log.debugstatements provide logging for when the script runs or skips execution.
Integrating This in Your Scheduled Script
You can place this code snippet at the beginning of your Scheduled Script to ensure it only executes on the last day of the month. If the condition is met, the script will proceed with whatever business logic you want to run.
Here’s how you can integrate this into your NetSuite Scheduled Script:
/**
* @NApiVersion 2.1
* @NScriptType ScheduledScript
* @NModuleScope SameAccount
*/
define(['N/log'], (log) => {
const execute = (scriptContext) => {
let today = new Date();
let todays_date = today.getDate();
let todays_month = today.getMonth() + 1; // JavaScript months are 0-indexed, so we add 1
let todays_year = today.getFullYear();
// Get the last day of the current month
let last = new Date(todays_year, todays_month, 0);
let last_date = last.getDate();
// Check if today is the last day of the month
if (todays_date === last_date) {
log.debug("Today is the last day of the month", `Executing end-of-month script.`);
// Place your business logic here for the end-of-month tasks
} else {
log.debug("Today is not the last day of the month", `Script will not run.`);
}
};
return { execute };
});
Scheduling the Script in NetSuite
Since NetSuite does not provide a direct “last day of the month” option for scheduling, you can set your script to run daily. The condition in your script will then ensure that it only runs its logic on the last day of the month.
Follow these steps to schedule the script:
- Go to Customization → Scripting → Script Deployments.
- Create a new Script Deployment for your Scheduled Script.
- Set the Schedule Type to Daily.
- The script will run daily, but the date-checking logic will ensure it only executes on the last day of the month.
Conclusion
By adding this simple date-checking logic to your Scheduled Script, you can effectively bypass the limitations of NetSuite’s scheduling options and run scripts only on the last day of the month. This approach is efficient and ensures that any critical end-of-month processes are handled without manual intervention.
This solution can be applied to various end-of-month processes such as financial reports, batch processing, invoicing, and reconciliations.