Skip to main content

ServiceNow Script Include & GlideAjax

What is Script Include?

Script includes are used to store JavaScript that runs on the server. Script Includes are reusable server-side script logic that define a function or class. Script Includes execute their script logic on Server Side only when explicitly called by other scripts whether from Server side or client side. Consider using script includes instead of global business rules because script includes are only loaded on request. 

There are different types of Script Includes:

On demand/classless 

Extend an existing class 

Define a new 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. The Script Include name must exactly match the name of the function. On demand Script Includes are typically used when script logic needs to be reused. Examples include standardizing date formats, enabling/disabling logging, and validating email addresses.

For Example, Script Include Name & function name will be same.

Script Include Name : getEmailId

function : getEmailId() {

var user = gs.getUserId();

var userRecord = new GlideRecord('sys_user');

userRecord.get(user);

if(userRecord) {

return userRecord.getDisplayValue('email');

} }

Define a new Class Script Include - Utilities Script Includes typically define a new class and therefore use the automatically inserted script template. The initialize function is automatically invoked when JavaScript objects are instantiated from the Script Include. Any variable defined as part of the this object in the initialize function is known to all other functions in the Script Include.

ScriptIncludeName :getSpecificLocation 

var getSpecificLocation = Class.create();

getSpecificLocation.prototype = {

    initialize: function() {   },

function : getSpecificLocation() {

var grLoc = new GlideRecord('cmn_location');

grLoc.addQuery('country', 'India');

grLoc.query();

while(grLoc.next()) {

return grLoc.location + '';  }

type: 'getSpecificLocation'  };

Extended Script Include- Script Includes can extend existing Script Includes by adding new methods and non-method properties. Although most ServiceNow classes are extensible, the most commonly extended classes are: GlideAjax: 

make AJAX calls from Client Scripts 

LDAPUtils: add managers to users, set group membership, 

debug LDAP Catalog*: set of classes used by the Service Catalog for form processing and UI building

Let's try to understand that How to call Server side script through client side script in ServiceNow?

Example : How to populate the email id of requester for in any of catalog item using ScriptInclude & GlideAjax ? 

ScriptInclude :getDetails

var getDetails = Class.create();

getDetails.prototype = Object.extendsObject(AbstractAjaxProcessor, {

getValue : function()  {

var email_id;

var uid =this.getParameter('sysparm_requested_for');

var getUserD = new GlideRecord('sys_user');      

 getUserD.addQuery('sys_id',uid);

 getUserD.query();

if(getUserD.next())  {

        return getUserD.email;

} },

type: 'getDetails'

});

Catalog onchange client script :getData

function onChange(control, oldValue, newValue, isLoading) {

   if (isLoading || newValue == '') {

      return;   }

var ga = new GlideAjax('getDetails');

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

ga.addParam('sysparm_requested_for',g_form.getValue('requested_for'));

ga.getXML(callback);

function callback(response)  {

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

g_form.setValue('email_id',answer);

}  }


Comments

Post a Comment

Popular posts from this blog

Difference b/w After & Async Business Rule in ServiceNow

Let's try to understand difference b/w  After & Async Business Rule & some examples on Real time scenario of After Business Rule & Async Business Rule : 1.> After BR executes synchronously (It will wait for a result, once result is ready & get displayed then user will get control to do anything, user has to wait) but Async BR executes asynchronously (user will get control immediately, it will not halt the user and result will get displayed when its ready or get response from system, it will be executing in the background as per system scheduler). 2.>After BR is used to update information on related objects that need to be displayed immediately such as GlideRecord queries etc. whereas Async BR is used to update the information on related objects that do not need to be displayed immediately such as calculating metrics & SLA etc. 3.>We can use current & previous objects but We can’t use previous object in Async rule & current ob...

How to use getreference() with callback function in client Script | #ServiceNow

What is the use of getReference() ? getReference() gets the record used in another reference field. For example you have the requested_for (reference to the sys_user table) and with getReference, you will retrieve the User record and save this as a gliderecord into a variable. Why we use callback function with getReference?  callback function allow you to use asynchronous processing with your getReference call. Example: FetchCompany&phoneNo function onChange(control, oldValue, newValue, isLoading) {    if (isLoading || newValue == '') {       return;    }  var requester_user = g_form.getReference('requested_for', callback); function callback(requester_user) { g_form.setValue('email_id',requester_user.email); g_form.setValue('manager', requester_user.manager); } }

ServiceNow Interview Questions - real time

Let's see some ServiceNow Scenario based Interview Questions & Answers here, 1.> How to fetch latest 10 incident records ? var grIncident = new GlideRecord('incident');  gr.orderByDesc('sys_created_on');  gr.setLimit(10);  gr.query();  while (gr.next()) {  gs.print('check the :'+ grIncident.number);   } 2.>How can you populate caller_id in short Description on Problem form? Write Onchange client script on Problem table on change of caller Id field  var caller =g_form.getValue(‘caller_id’);  g_form.setValue(‘short_description’, caller); 3.>How to send notification to the user who is member of assigned group? You can write the below code in Advanced condition of Notification gs.getUser().isMemberOf(current.assignment_group); 4.> How can you populate the only users whose department is “Finance” in caller field on Incident form. Use Simple Reference Qualifier & select “department is “Finance”. You can write the code ...