Retrieve Accounting Period by Date

The Suitelet script is designed to retrieve the accounting period for a given date. It takes a specified date as input and searches for the corresponding accounting period within NetSuite. 

/**
 * @NApiVersion 2.x
 * @NScriptType Suitelet
 */

define(['N/search'], function(search) {
    
    function onRequest(context) {
        try {
            // Extract the date from the request parameters
            var invoiceDateStr = '05/16/2024'; // Example date string (modify as needed)
            var invoiceDate = new Date(invoiceDateStr);
            var invoiceMonth = invoiceDate.getMonth() + 1; // Adding 1 because getMonth() returns zero-based month index
            var invoiceYear = invoiceDate.getFullYear();


            // Get the start and end dates of the month
            var firstDayOfMonth = new Date(invoiceYear, invoiceMonth - 1, 1); // Subtracting 1 from month because Date() constructor expects zero-based month index
            var lastDayOfMonth = new Date(invoiceYear, invoiceMonth, 0);


            // Format the start and end dates as 'M/d/yy' strings
            var formattedFirstDayOfMonth = (invoiceMonth < 10 ? '0' : '') + invoiceMonth + '/01/' + invoiceYear;
            var formattedLastDayOfMonth = (invoiceMonth < 10 ? '0' : '') + invoiceMonth + '/' + lastDayOfMonth.getDate() + '/' + invoiceYear;


            // Define search filters to find the accounting period for the given month
            var accountingPeriodSearch = search.create({
                type: search.Type.ACCOUNTING_PERIOD,
                filters: [
                    ['startdate', 'onorafter', formattedFirstDayOfMonth],
                    'AND',
                    ['enddate', 'onorbefore', formattedLastDayOfMonth]
                ],
                columns: ['periodname', 'startdate', 'enddate', 'internalid'], // Add internalid column
                title: 'Accounting Period Search' // Optional title for the search
            });


            // Run the search
            var searchResults = accountingPeriodSearch.run().getRange({
                start: 0,
                end: 1 // Retrieve only the first result
            });


            // Check if search returned any results
            if (searchResults.length > 0) {
                // Retrieve accounting period details from the first result
                var accountingPeriod = searchResults[0];
                
                // Log accounting period details including internal ID
                log.debug('Accounting Period Name', accountingPeriod.getValue('periodname'));
                log.debug('Start Date', accountingPeriod.getValue('startdate'));
                log.debug('End Date', accountingPeriod.getValue('enddate'));
                log.debug('Internal ID', accountingPeriod.id); // Retrieve internal ID
            } else {
                log.debug('No Accounting Period Found', 'No matching accounting period found for the given month.');
            }
        } catch (ex) {
            log.error('Error', ex.message);
        }
    }


    return {
        onRequest: onRequest
    };
});


Leave a comment

Your email address will not be published. Required fields are marked *