Glide AJAX Script Returning "Undefined" Into SCTASK Variables
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-16-2018 07:43 AM
Hello Dev Community,
I'm a JavaScript noob and could use some help understanding why the following combination of Script include and Client Scripts are returning "undefined" into some variables I've set up.
I'm attempting to pull down the Phone Number (phone_number) and Phone Name (name) from a CI (which is located on the cmdb_ci_comm table) on the SCTASK form and place these values into some variables I've set up. Thanks much in advance for the assistance.
Script Include:
var loanerPhoneExtract = Class.create();
loanerPhoneExtract.prototype = Object.extendsObject(AbstractAjaxProcessor, {
//Number Extract Function to be called in the Client script
numberExtract: function () {
var loanerPhoneNumber = this.getParameter('sysparm_loanerPhoneNumber');
var commDeviceTable = new GlideRecord('cmdb_ci_comm');
if (commDeviceTable.get(loanerPhoneNumber)) {
var phone = new GlideRecord('cmdb_ci_comm');
if (phone.get(commDeviceTable.parent)){
var json = new JSON();
var results = {
"sys_id": phone.getValue("sys_id"),
"phone_number": phone.getValue("phone_number")
};
return json.encode(results);
}
} else {
return null;
}
},
//Name Extract Function to be called in the Client script
nameExtract: function () {
var loanerPhoneNumber = this.getParameter('sysparm_loanerPhoneNumber');
var commDeviceTable = new GlideRecord('cmdb_ci_comm');
if (commDeviceTable.get(loanerPhoneNumber)) {
var phone = new GlideRecord('cmdb_ci_comm');
if (phone.get(commDeviceTable.parent)){
var json = new JSON();
var results = {
"sys_id": phone.getValue("sys_id"),
"name": phone.getValue("name")
};
return json.encode(results);
}
} else {
return null;
}
},
type: 'loanerPhoneExtract'
});
Client Script 1
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('loanerPhoneExtract');
ga.addParam('sysparm_name', 'nameExtract');
ga.addParam('sysparm_loanerPhoneNumber', g_form.getValue("cmdb_ci"));
ga.getXML(extractName);
}
function extractName(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var clearvalue; // Stays Undefined
if (answer) {
var returneddata = answer.evalJSON(true);
g_form.setValue("loanerPhoneName", returneddata.sys_id, returneddata.name);
} else {
g_form.setValue("loanerPhoneName", clearvalue);
}
}
Client Script 2
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('loanerPhoneExtract');
ga.addParam('sysparm_name', 'numberExtract');
ga.addParam('sysparm_loanerPhoneNumber', g_form.getValue("cmdb_ci"));
ga.getXML(extractNumber);
}
function extractNumber(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
var clearvalue; // Stays Undefined
if (answer) {
var returneddata = answer.evalJSON(true);
g_form.setValue("loanerPhonePhoneNumber", returneddata.sys_id, returneddata.phone_number);
} else {
g_form.setValue("loanerPhonePhoneNumber", clearvalue);
}
}
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-25-2018 02:55 AM
On the first look I did not find any bug in the code.
So the first point is to find out if the issue is on client or server side code. Try to put logging statements in the script include via gs.log shortly before return statement to see if the code actually got there and what were the values in the server code.
Then do the testing with opened browser developer tools to see the communication between Server and Client where you could see what "answer" was returned if any. But also you will see if even the proper parameter values were passed to the server.
By that you should find out if the issue is in Server Script or Client Script and then continue with debugging.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2018 10:10 AM
I know this is a few months old but to try and solve the problem. I would start by not using an undefined variable as a value to set to a form value. Instead, try using the g_form.clearValue('variable_name_goes_here') function. You could also try setting it to an empty string but I would stick with clearValue as the application of it makes sense in this context.
Your field will keep being populated as 'undefined' because that's exactly what the 'clearValue' variable is equivalent to if you do not initialize the value.
If this helps, please mark this answer as correct so others may use this information.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-18-2018 01:43 PM
As @coleton mentions, this is an older post. Were you able to resolve your issue?
I see a couple issues with the JSON bits and the logic within the Script Include as well. You are querying the "cmdb_ci_comm" table twice in order to get the "parent", but there is no "parent" field on that table.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-05-2018 11:33 PM
Did you find a solution to your issue?