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-15-2019 10:53 AM
Your class is RRcourseinfo, but the line is RRCourseCall. It should also be RRcourseinfo.
Also, you have no concept of current in the script include. You have to pass the info back to the client and use client g_form to populate the fields.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2019 11:00 AM
Thank you very much for spotting that inconsistency it was right in front of me. Do you know of any examples of pass the info back I can reference? The shallowness of my coding experience is showing unfortunately.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-15-2019 11:11 AM
I would have an object:
var obj = {};
Then put in the object what you want to return:
var parsedResponse = JSON.parse(responseBody);
obj.key = x_unoci_rapid_retu_quarter;
obj.value = parsedResponse.qtrName;
obj.key = x_unoci_rapid_retu_course_name;
obj.value = parsedResponse.name;
obj.key = x_unoci_rapid_retu_total_students;
obj.value = parsedResponse.numStudents;
obj.key = x_unoci_rapid_retu_quartershortcut;
obj.value = parsedResponse.qtr;
Then return a string representation of the object:
var objString = JSON.stringify(obj);
return objString;
Then your answer is now a string that looks like an object. We turn it back into an object in the callback of the client script:
var obj = JSON.parse(answer);
Loop through the new object:
for(var i=0 ; i < obj.length ; i++){
g_form.setValue(obj[i].key, obj[i].value); // and set the values
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2019 10:51 AM
Hello Mike,
Your example is incredibly helpful! I have modified my two scripts and I believe I am on the edge of success. I have run into an issue since modifying the script and I think I have narrowed down the culprit. I believe I am not properly defining "answer" anymore.
This is what the client script looks like now.
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");
//alert(response);
function RRCallParse(response) {
var obj = JSON.parse(answer);
console.log("GlideAjax: Rapid Return Course Call -> RRCallParse, answer is: " + obj);
for(var i=0;i<obj.length;i++){
g_form.setValue(obj[i].x_unoci_rapid_retu_quarter);
g_form.setValue(obj[i].x_unoci_rapid_retu_course_name);
g_form.setValue(obj[i].x_unoci_rapid_retu_total_students);
g_form.setValue(obj[i].x_unoci_rapid_retu_quartershortcut);
}
}
console.log("GlideAjax: x_unoci_rapid_retu.Rapid Return Course Call finished");
}
I get the feeling if it was a snake I'd be a dead man.
Edit* Now that I think about it some more, I think the issue is on the script include side. Am I not defining and packaging the information correctly? I suspect this may be the case. Is it a problem with how I defined the objects?
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;
alert("hello");
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 obj = {};
var parsedResponse = JSON.parse(responseBody);
obj.key = x_unoci_rapid_retu_quarter;
obj.value = parsedResponse.qtrName;
obj.key = x_unoci_rapid_retu_course_name;
obj.value = parsedResponse.name;
obj.key = x_unoci_rapid_retu_total_students;
obj.value = parsedResponse.numStudents;
obj.key = x_unoci_rapid_retu_quartershortcut;
obj.value = parsedResponse.qtr;
var objString = JSON.stringify(obj);
return objString;
}
current.update();
},
type: 'Rapid Return Course Call'
});