scenarios of using script include

sasi
Tera Contributor

Hi,

can anyone please explain script include how it will work. and explain one scenario where we can use this with scripting. please please don't send me links. i have tired to check with links but not able to understand. explain one scenario and from where we can call.

 

Regards,

Sasi.

1 ACCEPTED SOLUTION

Joel Millwood2
Kilo Guru

Hi Sasi,

 

A script include is simply a place to store some reusable JavaScript code that you would like to use usually more than once / in multiple places. If you have ever found yourself writing some JavaScript code in a Business Rule or a Client Script more than once for example, you could instead create a script include and call that script include when required from the Business Rule or the Client Script.

Out of the box there are over 1400 Script Includes included in the Madrid Release of ServiceNow that you can review by navigating to System Definition > Script Includes. 

In regards to other scenarios where you may use this functionality, consider a scenario where several of your catalog items may require the same functionality (e.g. populating specific employee data automatically when an employee is selected). Rather than scripting this functionality for each catalog item in a Catalog Client Script you could do it once in a Script Include which you could call from your Catalog Client Script for each Catalog Item. By doing this, you are reducing the risk of introducing errors and if you need to update any functionality you can do it once in a single place in the Script Include rather than individually in each Catalog Client Script.

View solution in original post

5 REPLIES 5

psphanindrakuma
Tera Guru
Tera Guru

Hi Sasi!

 

 Script include are piece of reusable JavaScript code run on server side. SI(script  include)  can called from any other server side scripts or client side scripts.They are most commonly used as an alternative to Global Business Rules, since they are only loaded upon request. For example, Script Includes are most frequently called from:

  • Business Rules
  • UI Actions
  • Reference Qualifiers
  • Transform Maps
  • Other Script Includes

 

Create a client script and add the below code. when the client script will run user ID will be alerted.

var ga = new GlideAjax('UserID');

ga.addParam('sysparm_name','loginuser');

ga.getXML(myid);

function myid(response) {

    var answer = response.responseXML.documentElement.getAttribute("answer");

    alert(answer);

}

Create a Scrite include with name UserID and check true client.

var UserID = Class.create();

UserID.prototype = Object.extendsObject(AbstractAjaxProcessor, {


    loginuser: function() {

          return gs.getUserID();
    },    
});

 

 

Please Mark as helpful if my answer helped you

 

 

 

 

Hi psphanindrakumar,

 

thank you so much for the help, can u please explian the scenario , consider a scenario where several of your catalog items may require the same functionality (e.g. populating specific employee data automatically when an employee is selected).

 

Regards,

Sasikala.

Joel Millwood2
Kilo Guru

Hi Sasi,

 

A script include is simply a place to store some reusable JavaScript code that you would like to use usually more than once / in multiple places. If you have ever found yourself writing some JavaScript code in a Business Rule or a Client Script more than once for example, you could instead create a script include and call that script include when required from the Business Rule or the Client Script.

Out of the box there are over 1400 Script Includes included in the Madrid Release of ServiceNow that you can review by navigating to System Definition > Script Includes. 

In regards to other scenarios where you may use this functionality, consider a scenario where several of your catalog items may require the same functionality (e.g. populating specific employee data automatically when an employee is selected). Rather than scripting this functionality for each catalog item in a Catalog Client Script you could do it once in a Script Include which you could call from your Catalog Client Script for each Catalog Item. By doing this, you are reducing the risk of introducing errors and if you need to update any functionality you can do it once in a single place in the Script Include rather than individually in each Catalog Client Script.

Buddy2
Giga Contributor

Hi, 

You will get the complete understanding of script include with practical example in below link:

 

Script Include in ServiceNow

 

Please mark my answer correct and helpful if my answer helped u. Thank you.