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

suzanneswanson
Kilo Guru

Thank-you, Paul.  The initialize function was not the problem (I have put it back in).  The new global.json fixed the issue.  David - you were on the right track, but I have to give the correct answer to Paul since he provided the code and I just had to plug and play.  It worked right away.

Give David a Helpful for his contribution 🙂


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

Ha thanks! 🙂

 

I agree the plug and play code makes it easier to implement the fix. Just glad to help.