1

I really hate the way netsuite suite script 2.0 handles date formatting. So, I would like to use Moment.js for parsing dates that I get back from a web service call. How do I get this to work inside a suite script 2.0?

As erictgrubaugh stated, I needed to add the moment.js library to the NetSuite file cabinet and then in the debugger use the library. Also note that my script is in File Cabinet -> SuiteScripts -> Libraries

But even so, I was having problems with the syntax. Because when I use the debugger it now doesn't throw any errors, but I can't step through any of the code and a log statement doesn't print anything.

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

define ([
 'N/log',
 '../Libraries/moment',
 ],

function(log, moment) {





var a = moment('2016-01-01'); 
var b = a.clone().add(1, 'week'); 
log.debug(b);

});

3 Answers 3

6

In general, the external library simply needs to be an AMD-formatted module, and you can include it directly in your code. If the library you want to use isn't AMD-compatible, there is extra work to do.

moment is AMD-compatible, so all you need to do is put the moment source file in your File Cabinet somewhere, then include it in your dependencies by its path.

require(["path/to/moment"], function (moment) {
    // use moment as usual
});

Here's a working example where I have moment.min.js in a sibling lib directory: https://gitlab.com/stoicsoftware/netsuite-sku-analytics/blob/master/FileCabinet/SuiteScripts/sku-analytics/total-monthly-by-sku/sa-TotalMonthlyBySku.js

Sign up to request clarification or add additional context in comments.

5 Comments

Actually I don't think it works. Because even though the debugger doesn't throw any errors, I can't step through the code.
I rarely use the debugger myself, but perhaps there will be others who can add input as well.
this works in a UE script. define(['N/record', 'N/log', 'N/ui/serverWidget', './moment.min.js'], function(record,log,ui,moment){} I'll echo Eric here. I find the debugger generally less than productive.
That's strange you needed the .js portion of the path; I've never seen that be necessary, though I know there are changes to the require library in 2017.1. Has your environment been updated @bknights?
Didn't try without it.
4

You said:

Because when I use the debugger it now doesn't throw any errors, but I can't step through any of the code and a log statement doesn't print anything.

But from the documentation:

Note If you need to ad-hoc debug your code in the NetSuite Debugger, you must use a require() function. The NetSuite Debugger cannot step though a define() function.

Comments

0

I can , in debugger:

require(["SuiteScripts/moment"], function (moment) {

var __d= moment().format(); 
var x =0; //breakpoint
});

enter image description here

this works

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.