How to refer a JS library file and call its functions from both Client and Server side scripts?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-22-2019 06:33 AM
I want to put my common code in a library file and access it across Service Now application in both Client and Server side scripts.
Suppose, I have this common function in an external file. I want to call this from both Client and Server scripts.
function truncateString(input){
var output = input;
if(input && input.length>10)
{
output = input.substring(0,10);
output += '...';
}
return output;
}
Please let me know.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-23-2019 07:41 PM
You need to use script includes functionality.
you can call script include from client and server side scripts.
https://community.servicenow.com/community?id=community_blog&sys_id=884d6ee5dbd0dbc01dcaf3231f9619c0
Regards,
Sachin

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-23-2019 10:17 PM
Hi
You can put your functions in Script Includes and mark the field "Client callable" as active (see screenshot below):
You then can call the functions inside of your scripts like:
var myScriptInclude = new DirkTest;
myScriptInclude.test();
Have fun!
If that answered your question, let me know and mark my answer as correct and helpful,please-
BR
DIrk

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2019 06:43 AM
Hi Dirk,
Thanks for the response.
I want to call the function which is in an External Library. I want to refer the library first in ServiceNow and then call its function from anywhere in ServiceNow.
Thanks,
Ramesh