3

Our team is trying to use Moment.js in our instance, but can't seem to get it to work. Here are a couple questions we have about it:

  1. We noticed that there is a dependency out of the box called moment-timezone-with-data-2010-2020-v0.5, is this the same as moment.js? If so, does this mean we don't need to bring in moment.js as a new dependency?
  2. We tried using the above ootb dependency AND tried to bring in moment.js to use in a widget, and we keep getting a console error saying that moment is undefined. Can someone provide some instructions on how to correctly get either one of these dependencies to work?
  3. If we wanted to use moment.js on a platform business rule, what do we have to do to make that happen? Are you able to access a dependency via business rule?

Thanks!

Dave
  • 1,257
  • 2
  • 27
  • 58

4 Answers4

1

Here's how I was able to do it:

Create a Script Include with the following attributes:

  • script name is "moment" (it needs to have this exact name)
  • scope is either global or the same scope as your project (I used global)
  • set as client callable

Paste the code from MomentJS 2.22.1 into the script body.

To verify that you can access your Script Include, open a Background Script and run the following test code:

var calendar = moment().add(1, 'days').calendar();
gs.log("calendar test: " + calendar);

var dayCount = moment().diff('1809-02-12', 'days');
gs.log('Abraham Lincoln was born ' + dayCount + " days ago!");

To answer your question on moment-timezone-with-data-2010-2020-v0.5: no's it's not the same; here's a link to Moment Timezone which is a different library by the same organization.

As of the time of this post, 2.22.1 is the newest version that runs in ServiceNow. There are newer versions of MomentJS, but they're too new for the SN interpreter.

You can also create a UI Script with version 2.22.1.

Ryan Loggerythm
  • 2,877
  • 3
  • 31
  • 39
0

Load the code for Moment.js into a script include and then you can call that like any other script include.

If you are going to use the timezone functions you would need to rewrite the calls to moment from the timezone javascript to use the above script include.

John Warren
  • 3
  • 1
  • 3
0

moment.js

On April 1st, 2022, the most recent version of moment.js to work on SNOW San Diego is 2.22.1:

https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.1/moment.min.js


Any version > 2.22.1 will give you the following error:

Could not save record because of a compile error: JavaScript parse error at line (1) column (37954) problem = missing name after . operator (<refname>; line 1)



moment-timezone.js

On April 1st, 2022, the most recent version of moment-timezone.js to work in SNOW San Diego is 0.5.28:

https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.28/moment-timezone.min.js


Any version > 0.5.28 will give you the following error:

Could not save record because of a compile error: JavaScript parse error at line (1) column (236) problem = missing name after . operator (<refname>; line 1)
-1

sadly you can not use momentjs on the server side in ServiceNow. Here are the installation instruction for momentjs for Rhino (the javascript interpreter SNOW uses): https://gist.github.com/UnquietCode/5614860

As you can see you would need to write new Java classes which SNOW will not allow you to do.

On the Client on the other hand you can use it, just copy paste the "Browser" implementation and include it as a global ui script: https://momentjs.com/docs/#/use-it/browser/

Gordon Mohrin
  • 645
  • 1
  • 6
  • 17