Announcing the Global SNUG Board of Directors. Learn more here

Help
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
asifnoor
Kilo Patron
Kilo Patron

Script Includes are mainly reusable server-side script logic that defines a function or class. Script Includes executing their script logic only when explicitly called by other scripts. It contains javascript code that runs on the server. Script Includes are only loaded on request and hence they are more preferable to use instead of global business rules.

There are mainly 3 types of Script Includes

  • On Demand/ Classless
  • Define a new Class
  • Extend an existing class

On-Demand/ Classless Script Include

A Script Include that defines a single function is known as an on-demand, or classless Script Include. The function is callable from other server-side scripts. On-demand Script Includes can never be used client-side even if the Client callable option is selected.

They are very helpful when you want to reuse the script logic. Examples of such script includes are standardizing date formats, enabling/disabling logging, and validating common fields such as email, phone, etc.

The name of the script include should match with the name of the function. Below is the screenshot of the sample on-Demand script include.

find_real_file.png

And you can call this script include like below either from background script or from any other script include for which it has access to.

new testScriptInclude();

Define a New Class

Find below the basic SI which has 2 functions. One returns the current state of the incident and another returns whether the incident is active or not.

find_real_file.png

you can call the newly created SI like below from background script or other SI like below

new isActiveIncident().isActive("INC0000055");
new isActiveIncident().getCurrentState("INC0000055");

If you want to call this script include from the client, then here is how you can do it.

var ga = new GlideAjax('isActiveIncident'); //SI name to come here.
 ga.addParam('sysparm_name','isActive');
 //if you want to pass incident number form clinet script, then you can call liek below
 //ga.addParam('sysparm_number',"INC0000055");
 //and in SI, you can access like this
 //var inc=this.getParameter('sysparm_number'); 

  ga.getXML(HelloWorldParse);
 

 function HelloWorldParse(response) {
   var answer = response.responseXML.documentElement.getAttribute("answer");
   g_form.addInfoMessage("Value is "+answer);
 }

Extend an existing class

This basically means extending an existing script include and creating your own child script include with your functions. Using Child SI, you can access all parent SI functions. For example, you wanted to add some custom functionality related to Incident and you noticed that there is already a script include that exists in the system related to incidents, which we have created above. But the one that you wanted to add is more custom and hence you do not want to edit the existing SI. Then, in this case, you will extend the existing script include.

Below is the screenshot of parent SI and the child SI which is extending the parent Script Include (isActiveIncident).

Parent SI

 

find_real_file.png

 

Child SI

find_real_file.png

 

Now here is how you can call this child SI and access parent SI functions.

//Call child script include like this
new sample_Extend_Script_Include().callChildFunction("Child SI");
//Call parent script include fucntion like this
new sample_Extend_Script_Include().getCurrentState("INC0000055");
new sample_Extend_Script_Include().isActive("INC0000055");

If you notice you are calling Parent SI functions similar to how you are calling child SI function. This is because your Child SI has extended the parent SI and it would mean that your child SI has access to all parent SI functions.

Kindly mark the article as helpful and bookmark if it helps.

Version history
Last update:
‎06-13-2019 12:48 AM
Updated by: