- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2022 08:23 AM
Hello,
I am trying to modify the existing script from GlideRecord to GlideAjax in OnChange Client Script. Below is the Code.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var gp = new GlideRecord('sys_choice');
gp.addQuery('dependent_value', newValue);
gp.addQuery('element', 'u_tm_model');
gp.orderBy('sequence');
gp.query(CallBack);
function CallBack(u_type) {
while (gp.next()) {
g_form.addOption('tm_model', gp.value, gp.label);
}
}
}
Thanks,
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2022 09:27 AM
Hello Mahesh,
I assume this is mostly your development exercise. Generally it is best to add options to the backend (instead of adding them on the fly) and then make your 2nd field dependent on 1st field. That way, you do not need to add/rmeove options on the fly. They will automaticlaly work. (Example check how assigned_to is dependent on assignment_group).
Here is how you can modify your scripts. Make changes if you encounter any error as i am providing you code on the fly.
Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax("myScriptUtils");
ga.addParam("sysparm_name","getOptions");
ga.addParam("sysparm_dependent_field",newValue);
var response = ga.getXMLAnswer(parseResponse);
function parseResponse(answer) {
if(answer != 'false') {
var options = JSON.parse(answer);
for(i=0;i<options.length;i++) {
g_form.addOption('tm_model', options[i].value, options[i].label);
}
}
}
Script Include
Client Callable: True
Name: myScriptUtils
var myScriptUtils = Class.create();
myScriptUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getOptions: function() {
var myval = this.getParameter("sysparm_dependent_field"); //This feield name shoudl match with field name in client script.
var gp = new GlideRecord('sys_choice');
gp.addQuery('dependent_value', myval );
gp.addQuery('element', 'u_tm_model');
gp.orderBy('sequence');
gp.query();
var arr=[];
while(gp.next()) {
var obj={};
obj.value = gp.value.toString();
obj.label = gp.label.toString();
arr.push(obj);
}
return JSON.stringify(arr);
},
type: 'myScriptutils'
});
Mark the comment as a correct answer and also helpful if this has helped to solve the problem.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2022 08:30 AM
Hello ,
Please replace glide record with below code
var ga = new GlideAjax('your script include name');
ga.addParam('sysparm_name', 'your method name in script incldue');
ga.getXML(callback);
Then create a script include and wrote your server login in the script include
please refer to below link on how to make a connection between client script and script include
https://community.servicenow.com/community?id=community_question&sys_id=94a60365db1cdbc01dcaf3231f96192d
Please mark this helpful or accept the solution if this helps you
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2022 08:49 AM
Client Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var gp= new GlideAjax('myScriptIncludeUtil');// you can use any other name for you SI
gp.addParam('sysparm_name', 'getModel');// this is your function name
gp.addParam('sysparm_newvalue', newValue);// a parameter you would like to send to your server script
gp.getXML(CallBack);
function CallBack(response) {
var answer = response.responseXML.getElementsByTagName("choices");
for(var i = 0; i < answer.length; i ++) {
g_form.addOption('tm_model', answer[i].getAttribute("value") , answer[i].getAttribute("label"));
}
}
}
Your script include logic:
Name: myScriptIncludeUtil
Client Callable: true
Function code:
var myScriptIncludeUtil = Class.create();
myScriptIncludeUtil.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getModel: function() { // build new response xml element for result
var newvalue = this.getParameter('sysparm_newvalue');
var gp = new GlideRecord('sys_choice');
gp.addQuery('dependent_value', newvalue);
gp.addQuery('element', 'u_tm_model');
gp.orderBy('sequence');
gr.query();
while(gr.next()){
this._addChoices(gr.value, gr.label);
}
},
// all items are returned to the client through the inherited methods of AbstractAjaxProcessor
_addChoices : function(name, value) {
var ch= this.newItem("choices");
ch.setAttribute("label",name);
ch.setAttribute("value",value);
},
type:"myScriptIncludeUtil"
});

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-04-2022 09:27 AM
Hello Mahesh,
I assume this is mostly your development exercise. Generally it is best to add options to the backend (instead of adding them on the fly) and then make your 2nd field dependent on 1st field. That way, you do not need to add/rmeove options on the fly. They will automaticlaly work. (Example check how assigned_to is dependent on assignment_group).
Here is how you can modify your scripts. Make changes if you encounter any error as i am providing you code on the fly.
Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax("myScriptUtils");
ga.addParam("sysparm_name","getOptions");
ga.addParam("sysparm_dependent_field",newValue);
var response = ga.getXMLAnswer(parseResponse);
function parseResponse(answer) {
if(answer != 'false') {
var options = JSON.parse(answer);
for(i=0;i<options.length;i++) {
g_form.addOption('tm_model', options[i].value, options[i].label);
}
}
}
Script Include
Client Callable: True
Name: myScriptUtils
var myScriptUtils = Class.create();
myScriptUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getOptions: function() {
var myval = this.getParameter("sysparm_dependent_field"); //This feield name shoudl match with field name in client script.
var gp = new GlideRecord('sys_choice');
gp.addQuery('dependent_value', myval );
gp.addQuery('element', 'u_tm_model');
gp.orderBy('sequence');
gp.query();
var arr=[];
while(gp.next()) {
var obj={};
obj.value = gp.value.toString();
obj.label = gp.label.toString();
arr.push(obj);
}
return JSON.stringify(arr);
},
type: 'myScriptutils'
});
Mark the comment as a correct answer and also helpful if this has helped to solve the problem.