NetSuite SuiteScript Functions

Here are a few simple functions that you can add to a script library. These are just a easy examples, but they may save you some time.

//==============================================================

//== START Formatting of Numbers

//===============================================================

/*

 * Pass in a number and this function will insert commas in the

 * appropriate places

 */

function numberWithCommas(x) {

var parts = x.toString().split(".");

parts[0] = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");

return parts.join(".");

}


//=============================================================

// Get the current Month Name and Quarter

//=============================================================

function getDateParams(){

/*

 * This function takes the current data

 * and returns the abrev. Month Name

 * and current Quarter

 * Return: month name and quarter

 */

   var dateArray=new Array();

   var d = new Date();

   var month = new Array();

       month[0] = "jan";

       month[1] = "feb";

       month[2] = "mar";

       month[3] = "apr";

       month[4] = "may";

       month[5] = "jun";

       month[6] = "jul";

       month[7] = "aug";

       month[8] = "sep";

       month[9] = "oct";

       month[10] = "nov";

       month[11] = "dec";

   var m = month[d.getMonth()];  //Get the name of the current month

   

   //** Get the Current Quarter

   if(m=='jan'||m=='feb'||m=='mar')

       {

       qtr='Q1';

       }

   else if(m=='apr'||m=='may'||m=='jun')

       {

       qtr='Q2';

       }

   else if(m=='jul'||m=='aug'||m=='sep')

       {

       qtr='Q3';

       }

   else{

       qtr='Q4'

   }

       

   dateArray[0]=m;

   dateArray[1]=qtr;

   

   return dateArray;

   

   

}

 

//****************************************************

// Pass a Date and an integer to add the number of days to the date

//*****************************************************

function addDays(theDate, days) {

   return new Date(theDate.getTime() + days*24*60*60*1000);

}

 //*************************************************************

//** FUNCTION: Working Days

//** Calculates the number of non-weekend days between 2 dates

//** If the dates are the same then 1 days is returned

//*************************************************************

function workingDaysBetweenDates(startDate, endDate) {     

 

   var dtstartDate=new Date(startDate);

   var dtendDate=new Date(endDate);

   // Validate input   


   if (dtendDate< dtstartDate) 

   return 0;       

   

   // Calculate days between dates   

   var millisecondsPerDay = 86400 * 1000;

   

   // Day in milliseconds   

   dtstartDate.setHours(0,0,0,1); 

   

   // Start just after midnight   

   dtendDate.setHours(23,59,59,999); 

   

   // End just before midnight   

   var diff = dtendDate - dtstartDate; 

   

   // Milliseconds between datetime objects       

   var days = Math.ceil(diff / millisecondsPerDay);       

   

   // Subtract two weekend days for every week in between   

   var weeks = Math.floor(days / 7);   

   var days = days - (weeks * 2);   

   

   // Handle special cases   

   var startDay = dtstartDate.getDay();   

   var endDay = dtendDate.getDay();       

   

   // Remove weekend not previously removed.      

   if (startDay - endDay > 1){

       days = days - 2;          

   }  

   

   // Remove start day if span starts on Sunday but ends before Saturday   

   if (startDay == 0 && endDay != 6){

       days = days - 1;  

   }

   

   // Remove end day if span ends on Saturday but starts after Sunday   

   if (endDay == 6 && startDay != 0) {

       days = days - 1;

   }

   return days;

   }

 


To view or add a comment, sign in

More articles by Mark Kreminski

  • Exploring NetSuite + AI: My Early Impressions and Experiments

    After tuning into the keynotes and presentations from SuiteWorld, one thing is crystal clear—NetSuite is diving deep…

  • NetSuite Customer Dashboards

    You can add a Custom Customer Dashboard for viewing important customer information. Each user can setup their own…

  • Navigating Success: The Role of Executive Dashboards in NetSuite

    In today’s world having a clear understanding of your organization’s performance is essential. With the advent of…

  • Merging Duplicates through Script

    NetSuite offers a variety of ways to deal with duplicate entities. You can setup duplicate detection to assist in…

    1 Comment
  • NetSuite Project 360 Dashboards

    NetSuite Projects 360 Dashboard New dashboard within NetSuite SuiteProjects brings an additional layer of visibility…

  • NetSuite Scripted Records

    What are Scripted Records Scripted Records are any record in NetSuite that has a client script, user event script or…

  • Getting Started with NetSuite part 1

    Getting Started with NetSuite Logging in You should have received an email from either the support desk or from…

  • Gathering Customer Information

    Online Forms, Suitelets and Processing Records Online Custom Record Forms • An online custom record form is used to…

    1 Comment
  • Saved Searches limiting results

    Limiting Saved Search Results We recently had a request to create a saved search that monitor the number of emails…

  • Limits and Governance

    Overcoming Scheduled Script Limitations Recently when creating a scheduled script I ran into 2 limitations. Overcoming…

    1 Comment

Others also viewed

Explore content categories