Watch Video to Learn How to Automatically Set NetSuite’s Reversal Entry Date

This article is relevant if you want to see how to manipulate NetSuite with SuiteScript 2.0 while driving a simple request to modify accounting dates on transactions based on user input.

Background

The Controller for one of our clients asked us to help them streamline a frequent accounting operation. The client heavily uses reversing journal entries for managing revenue and expenses. Since this is a repetitive monthly operation, their procedure is to copy last month’s entries, modify the dates and update the values. Thus last month’s entries act as a template for this month.

The client’s pattern is to make the first day of the following period be the reversing entry date. The desire is to have the journal entry do this automatically by seeing that last month’s entry had a value in the reversing entry field.

Demonstration of the Automatic Reversing Entry Date

This short demonstration video (1:37) Β is a conversation between myself and one of our technical consultants. This relatively simple function is easy to understand and represents a good way to learn SuiteScript 2.0 patterns.

Server Side BeforeLoad UserEvent SuiteScript 2.0

This code snippet was used to drive looking up the accounting period end date to then do some date math to determine the date of the first day of the next period. Here, we are priming the record as it loads from the server.

/**
 * @NApiVersion 2.x
 * @NScriptType UserEventScript
 * @NModuleScope Public
 */
define(['N/email', 'N/error', 'N/search', 'N/file', 'N/record', 'N/runtime', 'N/transaction'],
		
		
function(search) {
    
	var scriptFileName = "PRI_UE_JournalEntryCopy.js";
	
    function beforeLoad(scriptContext) {
    	
    	var scriptFunctionName = "beforeLoad";
    	var scriptFullName = scriptFileName + "--->" + scriptFunctionName;
    	log.debug(scriptFullName, "UserEventType: " + scriptContext.type);
        
        if (scriptContext.type != 'copy') { return; }

		var vPostingPeriod = scriptContext.newRecord.getValue("postingperiod");
		var vReversalDate = scriptContext.newRecord.getValue("reversaldate");
    	log.debug(scriptFullName, "vPostingPeriod:" + vPostingPeriod + ",   vReversalDate:" + vReversalDate);

		
		if (vReversalDate == '') { return; }
		//get the end date of the current period from the copy
        var objAccountingPeriodSearch = search.create({   'type':'accountingperiod'
			,'filters':[ ['internalid'    ,search.Operator.ANYOF  ,vPostingPeriod    ]                                 
                       ]  
            ,'columns':[ 'enddate'   ] 		
            });

		var AccountingPeriodSearch = objAccountingPeriodSearch.run();
        if (AccountingPeriodSearch.length == 0) { log.debug(scriptName, "No AccountingPeriods found "); }
        
    	if (AccountingPeriodSearch.length != 0) {  
           var AccountingPeriodSearchResults = AccountingPeriodSearch.getRange(0,1); 
           var vEndDate = AccountingPeriodSearchResults[0].getValue('enddate')    	
           var vNewReversalDate = new Date(vEndDate);
           //add one day to the end date to get to the first day of the next period
           vNewReversalDate.setDate(vNewReversalDate.getDate() + 1);
           scriptContext.newRecord.setValue("reversaldate",vNewReversalDate);
        }
    }
   return {
        beforeLoad: beforeLoad,
    };
});

Client Side SuiteScript 2.0

This NetSuite code snippet runs client side to drive the interactive user experience to automatically adjust the reversing date based on the current period. The code fires as the transaction date is changed on the client.

/**
 * @NApiVersion 2.x
 * @NScriptType ClientScript
 * @NModuleScope Public
 */
 
define(['N/search'],
		
function(search) {

	var scriptName = 'PRI_CS_JournalEntry.js';

    function fieldChanged(context){
        var rcd = context.currentRecord;
        var fieldId = context.fieldId;
        
        if (fieldId == 'trandate') {
        	var vTranDate = rcd.getValue({ fieldId: 'trandate' });
        	var vReversalDate = rcd.getValue({ fieldId: 'reversaldate' });
        	
        	if (vReversalDate == '') { return; }
        	
    		var objTranDate = new Date(vTranDate);
    	    var month    = '' + (objTranDate.getMonth() + 1);
    	    var day      = '' + objTranDate.getDate();
    	    var year     = '' + objTranDate.getFullYear();
    		if (month.length < 2) { month = '0' + month; }
    	    if (  day.length < 2) { day   = '0' + day;   }
    	    var SearchTranDate = month + "/" + day + "/" + year;
       	
    		//find the accounting period so we can get the end date 
            var objAccountingPeriodSearch = search.create({   'type':'accountingperiod'
    			,'filters':[         ['enddate'     ,search.Operator.ONORAFTER   ,SearchTranDate    ] 
    			            ,'AND'  ,['startdate'   ,search.Operator.ONORBEFORE  ,SearchTranDate    ]
    			            ,'AND'  ,['isadjust'    ,search.Operator.IS          ,'F']
    			            ,'AND'  ,['isquarter'   ,search.Operator.IS          ,'F']
    			            ,'AND'  ,['isyear'      ,search.Operator.IS          ,'F']
                           ]  
                ,'columns':[ 'enddate' ,'startdate'  ] 		
                });

    		var AccountingPeriodSearch = objAccountingPeriodSearch.run();
            var AccountingPeriodSearchResults = AccountingPeriodSearch.getRange(0,1000); 
           
            if (AccountingPeriodSearchResults.length == 0) { console.log( "No AccountingPeriods found "); }
            
        	if (AccountingPeriodSearchResults.length != 0) {  
        	   var vEndDate = AccountingPeriodSearchResults[0].getValue('enddate');
               var vNewReversalDate = new Date(vEndDate);
               //add one to the end date to get the first day of the next period
               vNewReversalDate.setDate(vNewReversalDate.getDate() + 1);
               rcd.setValue("reversaldate" ,vNewReversalDate);
            } 
        }
        
    } // function fieldChanged(context)
    return {
        fieldChanged: fieldChanged
    };
});

Innovate on the NetSuite Platform

This short video and code example illustrates the power of the NetSuite platform. How many times do we perform repetitive tasks that can be automated with a little bit of script and effort? If you have a NetSuite challenge that would like streamlined, let’s have a conversation.

Be Sociable, Share!

Marty Zigman

Holding all three official certifications, Marty is regarded as the top NetSuite expert and leads a team of senior professionals at Prolecto Resources, Inc. He is a former Deloitte & Touche CPA and has held CTO roles. For over 30 years, Marty has produced leadership in ERP, CRM and eCommerce business systems. Contact Marty to set up a conversation.

More Posts - Website - Twitter - Facebook - LinkedIn - YouTube

| Tags: , , | Category: Accounting, NetSuite, Technical | Leave a comment

Leave a Reply

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