- 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 06:53 AM
Harish,
If i want this to be done in Onsubmit, what can be done?
newValue should be replaced with what variable?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2022 06:14 AM
Hello Bala,
From below script are you getting the correct value in last alert message??
alert("Check name" + "--" + reqNam);
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);
}
Please advise.
Regards,
Abhinay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2022 06:15 AM
Yes Abhinay, Im getting correct values
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2022 06:22 AM
Thank you for sharing. Now try below.
I modified the shared script in replies with a cheeky twist.
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var result1 = ""; //new var to store final result to set as value of multi line text
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()) {
count1++;
reqNam.push(gr.name.toString());
alert("Check name" + "--" + reqNam);
}
}
for(var i =0; i<reqNam.length;i++)
{
result1 = result1+'\n'+reqNam[i];
}
result1 = result1.trim();
g_form.setValue('u_request_roles_list', result1);
}
Hope this will help. Please advise.
Regards,
Abhinay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-09-2022 06:26 AM
HI Abi,
It shows count++ is not defined.