- 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:04 AM
use
reqNam.push(gr.name.toString());

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2022 04:11 AM
Hi see the bold line
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);
}
}
}
Harish
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2022 04:15 AM
Tried this Harish, same issue.
Its updating only first selected name. After that if i add one more name in the list collector its still shows first name. not adding the second name.
Please help me to get this issue fixed.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2022 04:27 AM
I tested the below in PDI, Just change the fieldname to yours. It works well. Refer attached screenshot
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var array = [];
var listArray = g_form.getValue('list').split(',');
for (var i = 0; i < listArray.length; i++) {
var user = new GlideRecord('sys_user');
user.addQuery('sys_id', listArray[i]);
user.query();
while (user.next()) {
array.push(user.name.toString());
}
}
g_form.setValue('test', array);// set to test field
}
Harish