Scoped Application: Client Script is not calling the Script Include

suzanneswanson
Kilo Guru

Hello Community,

I have a scoped application in which I have a Client Script and a Script Include.

The client script uses GlideAJAX to call the Script Include.

If I make the Script Include to be in the Global app, then I can get the data back into the Client Script. 

But I get null when both the Client Script and the Script Include are in the scoped application.

I have run a background script on the Script Include and get data back.  So I believe the issue is that the Client Script is not connecting to the Script Include.  Any help would be very appreciated!

Here is my code:

Script Include:

find_real_file.png

var PFMUtilities = Class.create();
PFMUtilities.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
initialize: function() {
},

getPFMVersionInfo: function () {

   var gr = new GlideRecord('x_mpii_pfm1_pfm_version_and_section_information');
   gr.orderBy('pfm_version');
   gr.query();

   var PFMVersionARRAY = [];
   while (gr.next()) {
     PFMVersionARRAY.push ({
        PFMVersion : gr.getValue('pfm_version'),
        PFMVersionSysId : gr.getValue('sys_id')
      });
   }

   var json = new JSON();
   var data = json.encode(PFMVersionARRAY); //JSON formatted string
   return data;
},

   type: 'PFMUtilities'
});

 

The Client Script is:

function onLoad() {

   // get the list of PFM Versions and Sections
   // var ga = new GlideAjax('global.PFMUtilities'); // Script Include Name
   var ga = new GlideAjax('PFMUtilities'); // Script Include Name
   ga.addParam('sysparm_name', 'getPFMVersionInfo'); // function defined in the script include
   ga.getXML(informationParse); // The function to parse the return values
}

function informationParse (response) {
   g_form.addOption('PFMVersion', "", '--Select One--');

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

   for( var i = 0; i < answer.length ; i++) {
      //Loop through array returned from Script Include
      g_form.addOption('PFMVersion', answer[i].PFMVersionSysId, answer[i].PFMVersion);
   }
}

1 ACCEPTED SOLUTION

Good point David!

Try this code now:

var PFMUtilities = Class.create();
PFMUtilities.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

// Removed initialize function

getPFMVersionInfo: function () {

   var gr = new GlideRecord('x_mpii_pfm1_pfm_version_and_section_information');
   gr.orderBy('pfm_version');
   gr.query();

   var PFMVersionARRAY = [];
   while (gr.next()) { 
     PFMVersionARRAY.push ({
        PFMVersion : gr.getValue('pfm_version'),
        PFMVersionSysId : gr.getValue('sys_id')
      });
   }

   var json = new global.JSON(); //global prefix required in scope
   var data = json.encode(PFMVersionARRAY); //JSON formatted string

   return data; 
},

   type: 'PFMUtilities'
});

ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

View solution in original post

7 REPLIES 7

Yogi3
Kilo Guru

Try using below client script

 

function onLoad() {

   // get the list of PFM Versions and Sections
   // var ga = new GlideAjax('global.PFMUtilities'); // Script Include Name
   var ga = new GlideAjax('x_mpii_pfm1.PFMUtilities'); // Script Include Name
   ga.addParam('sysparm_name', 'getPFMVersionInfo'); // function defined in the script include 
   ga.getXML(informationParse); // The function to parse the return values
}

function informationParse (response) {
   g_form.addOption('PFMVersion', "", '--Select One--');

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

   for( var i = 0; i < answer.length ; i++) { 
      //Loop through array returned from Script Include
      g_form.addOption('PFMVersion', answer[i].PFMVersionSysId, answer[i].PFMVersion);
   }
}

The SN Nerd
Giga Sage
Giga Sage

Try removing your initialize function. 

This might be overriding the AbstractAjaxProcessor constructor and causing the script to fail.


ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022

DScroggins
Kilo Sage
As Paul stated remove the initialize function but also in order to use JSON().encode() in a scoped app you must declare with global like so: new global.JSON().encode() You can also use JSON.stringify() instead.

Good point David!

Try this code now:

var PFMUtilities = Class.create();
PFMUtilities.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {

// Removed initialize function

getPFMVersionInfo: function () {

   var gr = new GlideRecord('x_mpii_pfm1_pfm_version_and_section_information');
   gr.orderBy('pfm_version');
   gr.query();

   var PFMVersionARRAY = [];
   while (gr.next()) { 
     PFMVersionARRAY.push ({
        PFMVersion : gr.getValue('pfm_version'),
        PFMVersionSysId : gr.getValue('sys_id')
      });
   }

   var json = new global.JSON(); //global prefix required in scope
   var data = json.encode(PFMVersionARRAY); //JSON formatted string

   return data; 
},

   type: 'PFMUtilities'
});

ServiceNow Nerd
ServiceNow Developer MVP 2020-2022
ServiceNow Community MVP 2019-2022