- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2022 03:42 AM
Hi,
I am writing a script to populate the values from list collector to Multi line text field using catalog client script onchange function.
But, when the iteration is happening it is overriding the first name and updating 2nd time. Please help me to get this fixed.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var reqNam = [];
var reqForList = g_form.getValue('requestedfor');
alert("Req list " + reqForList);
var reqFor = reqForList.split(',');
alert("Requested for" + " " + reqFor);
for (i = 0; i < reqFor.length; i++) {
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', reqFor[i]);
gr.query(myCallbackFunction);
function myCallbackFunction(gr) {
while(gr.next()) {
reqNam.push(gr.name);
alert("Check name" + "--" + reqNam);
}
g_form.setValue('u_request_roles_list', reqNam);
}
}
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2022 06:00 AM
Hi Bala,
Here is the working code. Just change your field names and tablename in script include
getListCollector: function() {
var array = [];
var sysID = this.getParameter('sysparm_id'); // get values from client script
gs.info("listArray sysID"+sysID);
var user = new GlideRecord('sys_user');
user.addQuery('sys_id','IN',sysID); // sysid contains list collector values
user.query();
while (user.next()) {
array.push(user.name.toString());
}
gs.info("Array"+array);
return array.join(','); // return array
},
Client SCript:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('listCollector'); // SCript Include name
ga.addParam('sysparm_name', 'getListCollector'); // function name in SCript includes
ga.addParam('sysparm_id', newValue);// pass list collector values to script include
ga.getXML(getResponse);
}
function getResponse(response){
var answer= response.responseXML.documentElement.getAttribute("answer");
alert("answer"+answer);
g_form.setValue('test',answer); // set your variable name
}
Harish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2022 04:46 AM
Using GlideRecord is not recommended in client scripts + without a callback function is a complete no no. Not the best practice.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2022 04:55 AM
you can use the same logic and convert it to script include.
Harish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2022 04:40 AM
Try this --> Set Value outside loop
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var reqNam = [];
var reqForList = g_form.getValue('requestedfor');
alert("Req list " + reqForList);
var reqFor = reqForList.split(',');
alert("Requested for" + " " + reqFor);
for (i = 0; i < reqFor.length; i++) {
var gr = new GlideRecord('sys_user');
gr.addQuery('sys_id', reqFor[i]);
gr.query(myCallbackFunction);
}
function myCallbackFunction(gr) {
while(gr.next()) {
reqNam.push(gr.name.toString());
alert("Check name" + "--" + reqNam);
}
}
g_form.setValue('u_request_roles_list', reqNam);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2022 04:54 AM
Hi Survo,
I tested the same. Now no name is getting updated in variable Survo.
Is there any chances to use the same script from server side like glideajax and using client script can we set this?
Please can u help me to update using script include and client script to get this worked?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2022 05:00 AM
Follow this article
https://community.servicenow.com/community?id=community_article&sys_id=c77ff0dddbbb8190847d5ac2ca96198a
Send the reqForList as a parameter rest of the logic should be implemented in script include.