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

Ramesh Babu1
Kilo Contributor

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.

3 REPLIES 3

sachin_namjoshi
Kilo Patron
Kilo Patron

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

DirkRedeker
Mega Sage

Hi

You can put your functions in Script Includes and mark the field "Client callable" as active (see screenshot below):

 

find_real_file.png

 

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

 

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