SuiteScript Function Naming

Over time, everyone builds their own style of programming. A lot of that style is defined by your past experience, where and how you learn a particular concept, the context in which the code is being written and your goals when writing that code. There are many styles of programming and I would not consider any of them to be right or wrong because there are pros and cons to each way of getting the job done.

When writing scripts for NetSuite there are a few things to keep in mind. What do I name my function? When is the script going to execute? What is the script going to do? Let’s talk about naming our functions.
Read more

map() your NetSuite Search Results

Almost every script that I write for NetSuite performs a search for some kind of data. You can accomplish this easily with SuiteScript by using either nlapiLoadSearch(), which will run an existing saved search and return the results, or nlapiSearchRecord(), which is used to build your saved search in code.

I prefer the latter option since the script does not have the external dependency on the saved search. If anyone were to change or delete that search, the script may no longer perform as expected. Also, by defining the search within the script itself the search logic can be stored in your source code repository.

Both of these functions return an array of nlobjSearchResult. but these objects can be cumbersome to work with and frequently calling getValue() or getText() can introduce a bit of noise into your code making it more difficult to read. I prefer to take these results and convert them into an an array of standard Javascript objects. This can make your search results easier to work with. Fortunately, the Javascript map() function works perfectly for this scenario.  The map() function executes a callback function for each element in an array and creates a new array from the results of the callback function.

Read more

Do you have a Moment?

Working with dates in native javascript can quickly become tedious and cumbersome. You have the standard Date object with a few different methods for constructing that object. Then you have a series of getXXX and setXXX methods that let you work with and manipulate various pieces of that Date object.

But what if you need to do more than that? What if you need to calculate future dates from this object? How do you display your date as a relative time like ‘4 hours ago’ or ‘3 weeks ago’. How do you compare two different Date objects? You could start down a long road toward building your own library of utility methods to answer some of these questions or … Enter moment.js.

Read more