You are currently viewing Calling a script include from a client script in ServiceNow?

Calling a script include from a client script in ServiceNow?

This is one of the very much used thing in servicenow. You have to use Script Include to save time, run your code on server side & re-use certain functions.

In below example in have used the following –

  • On Load Client script named – GetCurrentTime
  • A Script Include – ClientDateTimeUtils
  • Script Include Function – getNowDateTime
  • A callback function – DateTimeParser

So, from the GetCurrentTime client script, we are calling the ClientDateTimeUtils script. This is done using GlideAjax, where under the sysparm_name parameter we have to pass the argument as the function name (getNowDateTime) of the script included. The callback function DateTimeParser retrieves responses from the script and displays an alert when the form loads.

Client script: GetCurrentTime
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getNowDateTime');
ajax.getXML(DateTimeParser);
function DateTimeParser(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);

Script Include:
var ClientDateTimeUtils = Class.create();
ClientDateTimeUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getNowDateTime: function(){
var now = gs.nowDateTime(); //Now Date/Time
return now;
},
    type: 'ClientDateTimeUtils'
});


//Output
13/11/2022 10:57:37

Leave a Reply