How to include an external JavaScript library in a UI Script?

ryannnnnnnnnnnn
Tera Contributor

Hi,

How can we include external libraries in UI Scripts?

Specifically, I want to use MomentJS from a UI Script of mine.

I've read in many places that it's possible to do this from either a Script Include or from UI Scripts, but there is never an accompanying guide for doing this.

Example with incorrect code: https://community.servicenow.com/community?id=community_question&sys_id=f9a898ebdb0033c0f7fca851ca96...

 

There's also a reference to this YouTube video: https://www.youtube.com/watch?v=JrVGodtzY3U

...but the code that he grabs from the website is not the code that he uses in his Script Include (21 minute, 20 second mark). He's altered it to remove the errors which keep me from simply saving the file

Thanks,

Ryan

1 ACCEPTED SOLUTION

ryannnnnnnnnnnn
Tera Contributor

Hi Joro,

Thank you 🙂

 

If anyone else needs this library in the future, the newest compatible version as of today is MomentJS 2.22.1.  Anything more recent will not work.

Also, the Script Include has to be called "moment", not "MomentJS", and not "moment.js".

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

 

Thanks,

Ryan

View solution in original post

4 REPLIES 4

Community Alums
Not applicable

Hey ryan,

there are several way - they one I use is to get the entire library (easiest by finding such from a SDN or to pack it with some tool if its lineMode.js). Then you just add it as UI Script or Script Include (you know it depends what type is it).

 

Then you just use it. I am using this approach to use Angular.js/Angular2/Reach.js from a long time - ServiceNow is using same approach actually).

 

Let me know if you have any queries 🙂

 

P.S. If its only coz of the errors - open the Sscript Includes in a list, expose in the list the script field. On your SI record, just double click, select all, paste the code you need - and just move the mouse out. No errors. Remeber to set the Client Callable to true ( ??guys , help here - I'm not mistaken here for the field, right ? As I cant recall it correctly at 01:00 in the morning 😛 ) 😉

Cheers,

Joro

ryannnnnnnnnnnn
Tera Contributor

Hi Joro,

Sorry, but I don't follow.  Could you please explain with an example?

For instance, this is the exact library I'm trying to use:

https://momentjs.com/downloads/moment.js

Thanks,

Ryan

Community Alums
Not applicable

Hi Ryan,

copy your moment.js as a plain text. Create script include, name it moment.js, delete everything in script field (empty script actually) and same that. Open list view with includes and addt the script field.

 

P.S. - It is highly possible to get errors again when you call this SI. I use webpack to get such libs in a needed format.

 

Cheers,

Joro

find_real_file.png

ryannnnnnnnnnnn
Tera Contributor

Hi Joro,

Thank you 🙂

 

If anyone else needs this library in the future, the newest compatible version as of today is MomentJS 2.22.1.  Anything more recent will not work.

Also, the Script Include has to be called "moment", not "MomentJS", and not "moment.js".

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

 

Thanks,

Ryan