API call through script include using client script
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2019 10:45 AM
Hello everyone,
I am using an onchange client script to pass 1 variable of data to a script include which will fire off a REST message to retrieve 4 other data points. My end goal is to take these 4 data points and enter them on the same form that started the process. Has anyone else had success doing this? Can I set the fields directly from the script include or do I need to somehow pass the data points back to the client script to set them? Currently I am able to call my script include successfully, but my script include is failing.
This is my client script. Which appears to be working as hoped.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('x_unoci_rapid_retu.Rapid Return Course Call');
ga.addParam('sysparm_name', 'RRCall');
ga.addParam('sysparm_x_unoci_rapid_retu_course_code', "");
alert("hello");
ga.getXML(RRCallParse);
alert(response);
function RRCallParse(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer); }
}
This is my script include which is failing.
var RRcourseinfo = Class.create();
RRCourseCall.prototype = Object.extendsObject(global.AbstractAjaxProcessor,{
RRCall: function() {
var input = this.getParameter('sysparm_x_unoci_rapid_retu_course_code');
var responseBody;
var requestBody;
var r;
var httpstatus;
try {
r = new sn_ws.RESTMessageV2('x_unoci_rapid_retu.EEE course information ', 'Default GET');
r.setStringParameterNoEscape('Course Code', input);
var response = r.execute();
var httpStatus = response.getStatusCode();
}
catch(ex) {
var message = ex.message;
}finally {
requestBody = r ? r.getRequestBody():null;
}
{
var parsedResponse = JSON.parse(responseBody);
current.x_unoci_rapid_retu_quarter = parsedResponse.qtrName;
current.x_unoci_rapid_retu_course_name = parsedResponse.name;
current.x_unoci_rapid_retu_total_students = parsedResponse.numStudents;
current.x_unoci_rapid_retu_quartershortcut = parsedResponse.qtr;
}
current.update();
},
type: 'Rapid Return Course Call'
});
The error I am getting is:
org.mozilla.javascript.EcmaError: "RRCourseCall" is not defined.
Caused by error in sys_script_include.67e8d3addb007f00712f389f9d961988.script at line 2
1: var RRcourseinfo = Class.create();
==> 2: RRCourseCall.prototype = Object.extendsObject(global.AbstractAjaxProcessor,{
3: RRCall: function() {
4: var input = this.getParameter('sysparm_x_unoci_rapid_retu_course_code');
5: var responseBody;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2019 01:46 PM
I have have tried something different, but I'm still not having any luck. I tried packaging the returned information like this to be moved to the client script.
var RRcourseinfo = Class.create();
RRcourseinfo.prototype = Object.extendsObject(global.AbstractAjaxProcessor,{
RRCall: function() {
var input = this.getParameter('sysparm_x_unoci_rapid_retu_course_code');
var responseBody;
var requestBody;
var r;
var httpstatus;
try {
r = new sn_ws.RESTMessageV2('x_unoci_rapid_retu.EEE course information ', 'Default GET');
r.setStringParameterNoEscape('Course Code', input);
var response = r.execute();
var httpStatus = response.getStatusCode();
}
catch(ex) {
var message = ex.message;
}
finally {
requestBody = r ? r.getRequestBody():null;
}
var parsedResponse = JSON.parse(responseBody);
var packagedXML = this.newItem("RRCallXMLResponse");
packagedXML.setAttribute("x_unoci_rapid_retu_quarter", parsedResponse.qtrName);
packagedXML.setAttribute("x_unoci_rapid_retu_course_name", parsedResponse.name);
packagedXML.setAttribute("x_unoci_rapid_retu_total_students", parsedResponse.numStudents);
packagedXML.setAttribute("x_unoci_rapid_retu_quartershortcut", parsedResponse.qtr);
},
type: 'Rapid Return Course Call'
});
then alerting myself as a test this way in the client script.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue == '') { return; }
console.log("GlideAjax: x_unoci_rapid_retu.Rapid Return Course Call beginning"); // log beginning of function
var ga = new GlideAjax('x_unoci_rapid_retu.Rapid Return Course Call');
ga.addParam('sysparm_name', 'RRCall');
ga.addParam('sysparm_x_unoci_rapid_retu_course_code', "");
console.log("GlideAjax: Rapid Return Course Call, finished addParam");
//alert("hello");
ga.getXML(RRCallParse);
console.log("GlideAjax: Rapid Return Course Call, getXML call finished");
function RRCallParse(response) {
var answer = response.responseXML.documentElement.getElementsByTagName("RRCallXMLResponse");
console.log("GlideAjax: Rapid Return Course Call -> RRCallParse, answer is: " + answer);
console.log("GlideAjax: Rapid Return Course Call -> RRCallParse length: " + answer.length);
}
console.log("GlideAjax: x_unoci_rapid_retu.Rapid Return Course Call finished");
}
Unfortunately I am not getting anything in the sys log. The console is telling me nothing useful either. It's triggering, but not getting anywhere.