Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Client script calling script include scope mismatch

chrisn_
Mega Guru

I have created a scoped application that I am allowing most access to. I have a catalog client script which is trying to call a script include and I am getting a Script: x_unoci_rapid_retu.RRCall not found in scope: x_unoci_rapid_retu, and HTTP Processor class not found: x_unoci_rapid_retu.RRCall: no thrown error.

The client script looks like this 

var ga = new GlideAjax('RRCall');

it is in an scoped application but the global checkbox is checked.

The script include:

var RRcourseinfo = Class.create();
RRcourseinfo.prototype = Object.extendsObject(AbstractAjaxProcessor,{
RRCall: function() {

The script include is also in the same scoped application but is accessible from all application scopes.

I have tried putting global.RRCall and the scope of the application x_unoci_rapid_retu in the client script but to no avail, and I have also tried putting global in the 2nd line of the script include here RRcourseinfo.prototype = Object.extendsObject(global.AbstractAjaxProcessor,{

I continue to get the same error each time.

22 REPLIES 22

That is the correct way to pass a parameter:

ga.addParam('sysparm_x_unoci_rapid_retu_course_code', '12345');

Obviously '12345' would be updated with the actual course code.

From there you can log the value retrieved by the script include if you wanted to verify that it is getting it like so:

var input = this.getParameter('sysparm_x_unoci_rapid_retu_course_code');
gs.log(input);

 

If you are able to verify that the value passed is making it to the script via the log then the error is with this line:

r.setStringParameterNoEscape('Course Code', input);

If your URL variable is ${input} then the line should be:

r.setStringParameterNoEscape('input', input);

Hi David,

I would like to preface this with a hearty thank you as your help has been critical in moving forward in this endeavor.

 I have followed your guidance above and tried the following. The course code is being defined properly i the client script, but I'm still not getting it in the script include, when the gs.log message shows up it's undefined.

 

Client Script call

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

if (isLoading || newValue == '') {
return;
}


var ga = new GlideAjax('RRcourseinfo');
ga.addParam('sysparm_name', 'RRCall');
ga.addParam('sysparmx_unoci_rapid_retu_course_code', '');
ga.getXML(RRCallParse);

 

function RRCallParse(response) {

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

g_form.setValue(x_unoci_rapid_retu_quarter,obj.quarterName);
g_form.setValue(x_unoci_rapid_retu_course_name,obj.name);
g_form.setValue(x_unoci_rapid_retu_total_students,obj.numStudents);
g_form.setValue(x_unoci_rapid_retu_quartershortcut,obj.qtr);


}

 

The script include is what is astounding me. 

If I try this one. It fires off the call, but is missing the key component to get me a valid response.

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

RRCall: function() {



try {
var r = new sn_ws.RESTMessageV2('EEE course information', 'Default GET');
var input = this.getParameter('sysparmx_unoci_rapid_retu_course_code');
r.setStringParameterNoEscape('Course Code', input);

var response = r.execute();
var requestBody = response.getBody();
var httpStatus = response.getStatusCode();
gs.info(JSON.stringify(responseBody));
var obj = {};

var parsedResponse = JSON.parse(requestBody);

obj.quarterName = parsedResponse.qtrName;
obj.name = parsedResponse.name;
obj.numStudents = parsedResponse.numStudents;
obj.qtr = parsedResponse.qtr;


var objString = JSON.stringify(obj);
return objString;

}catch(ex) {

var message = ex.message;
gs.error(message);

}



},


type: 'RRcourseinfo'
});

 

If I change the top portion to 

var input = this.getParameter('sysparm_x_unoci_rapid_retu_course_code');
gs.log(input);


try {
var r = new sn_ws.RESTMessageV2('EEE course information', 'Default GET');

r.setStringParameterNoEscape('input', input);

 

Then no API goes out at all. Nothing in the log. In the actual rest message my endpoint looks like this

https://blah.uc.blah/util/ccodevalidator/validate.php?ccode=${input}&name_type=long&quarter=F18&json=1

and I have the variable substitute for input below as well. I'm obviously missing something, but I am unsure as to what.

You are most certainly welcome. I'm happy to help. Now lets get you to the finish line. 🙂

In the client script you posted I do not see any value being passed to the script include using addParam().  

This is what you currently have:

ga.addParam('sysparmx_unoci_rapid_retu_course_code', '');

In the example provided I see that the value is blank, as well as, the parameter name is incorrect. It should start with 'sysparm_' and your example is starting with 'sysparmx_'. Can you try the following by updating the parameter name to 'sysparm_course_code' and by hard coding an actual course code (replace 12345 with actual code) like so:

ga.addParam('sysparm_course_code', '12345');

 

Then for your script include lets update the variable being defined, as well as, the getParameter() method to look for the new parameter like so:

var cc = this.getParameter('sysparm_course_code');
gs.log(cc);

var r = new sn_ws.RESTMessageV2('EEE course information', 'Default GET');
r.setStringParameterNoEscape('input', cc);

 

Notice I have changed the variable 'input' to 'cc' and then also updated the getParameter() method to look for the new parameter 'sysparm_cource_code'.

 

The setStringParameterNoEscape() method can now reference the URL variable that you have in your outbound REST message endpoint although I'd also suggesting changing it from 'input' as that can also be confusing. I might recommend using a variable like 'course_code' in the URL and then in the setStringParameterNoEscape() method like so:

URL:

https://blah.uc.blah/util/ccodevalidator/validate.php?ccode=${course_code}&name_type=long&quarter=F18&json=1

 

Script Include  with updated setStringParameterNoEscape() method:

var cc = this.getParameter('sysparm_course_code');
gs.log(cc);

var r = new sn_ws.RESTMessageV2('EEE course information', 'Default GET');
r.setStringParameterNoEscape('course_code', cc);

 

Make sure that script goes inside the 'try' block in your Script Include and you should now be good to go. 

Have you had a chance to try these changes? Wondering if you were able to get your script working.

 

If I have helped answer your question please mark as helpful or correct so that others may benefit.

Thanks!

 

Hi David,

You have been of immense help so far, but I have not been able to get this to work. When I made the above changes the api calls actually stopped firing entirely. The business rules were still trying to run, but the call itself was no longer going out. The changes we made should not have an affect on that part, but somehow it did. I'm currently working on plan B which is to import the data I'm trying to query to a custom table as I've unfortunately spent an inordinate amount of time trying to make this work without success (my own fault of course). I want to revisit this and get it working after the fact and eventually stop the data feed I'm setting up, but at least for version one of this application I'm going to have to forgo the live checking.