get user_name from List Collector (sys_user) and put user+user_name into hidden variable

Community Alums
Not applicable

I have a catalog item with a list collector that gathers multiple users. When that variable comes through on the ritm/task, it's just a list of names. What is desired is for their user_name to also appear. However, I don't want a separate field on the form that they see when they're filling this out; I only want it to be passed to the ritm/task after the form is submitted. I thought I'd make a multiline textbox variable that could fill in the users and their usernames in this format: 

    Bob User [bob123]
    Sally User [sally456]

by using a client script and script include. Neither is working and when I submit the form, I get 'Unhandled exception in GlideAjax'. Can you please look at what I have and help? 

Here is the client script (ui type=all; applies to cat item, ritm, task): 

function onSubmit() {
   //array of sys_ids from list collector variable 
    var usersToAdd = g_form.getValue('persons_requiring_access').toString().split(',');

    var ga = new GlideAjax('GetListCollectorVariables'); //call the script include
    ga.addParam('sysparm_name', 'UsersListCollector'); //name of function in script include
    ga.addParam('sysparm_users', usersToAdd); //pass array of users to SI
    ga.getXML(GetVariables);

    function GetVariables(response) {
        var answer = response.responseXML.documentElement.getAttribute('answer');
        g_form.setValue('persons_requiring_access_text', answer);
    }
   
}

Here is the script include (client callable = true; global scope) 

var GetListCollectorValues = Class.create();
GetListCollectorValues.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    UsersListCollector: function() {
		var userInfo = '';
		var usersFromListCollector = this.getParameter('sysparm_users');
		var grUser = new GlideRecord('sys_user');
		grUser.addQuery('sys_id','IN',usersFromListCollector);
		grUser.query();
		while(grUser.next()) { 
			userInfo += grUser.name + ' [' + grUser.user_name + ']';
		}
		return info.toString();
    },
    type: 'GetListCollectorValues'
});
1 ACCEPTED SOLUTION

remove this one only glide_list=true

View solution in original post

27 REPLIES 27

Harshit Jamwal1
Mega Guru

 

Hi Annie, You should return info.toString();

var GetListCollectorValues = Class.create();
GetListCollectorValues.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
    UsersListCollector: function() {
		var userInfo = '';
		var usersFromListCollector = this.getParameter('sysparm_users');
		var grUser = new GlideRecord('sys_user');
		grUser.addQuery('sys_id','IN',usersFromListCollector);
		grUser.query();
		while(grUser.next()) { 
			userInfo += grUser.name + ' [' + grUser.user_name + ']';
		}
		return userInfo.toString();
    },
    type: 'GetListCollectorValues'
});

Harsh Vardhan
Giga Patron

This is what i have tested and working. 

Please try 

 

onSubmit Catalog client script:

 

function onSubmit() {
	//Type appropriate comment here, and begin script below
	//Type appropriate comment here, and begin script below
	var ga = new GlideAjax('CommTes'); //call the script include
	ga.addParam('sysparm_name', 'UsersListCollector'); //name of function in script include
	ga.addParam('sysparm_users', g_form.getValue('my_test')); //pass array of users to SI
	ga.getXML(GetVariables);

	function GetVariables(response) {
		var answer = response.responseXML.documentElement.getAttribute('answer');

		alert(answer);
		return false;
		//g_form.setValue('persons_requiring_access_text', answer);
	} 

}

 

Script Include:

 

var CommTes = Class.create();
CommTes.prototype = Object.extendsObject(AbstractAjaxProcessor, {

	UsersListCollector: function() {
		var obj = [];
		// 		var json = new JSON();
		var usersFromListCollector = this.getParameter('sysparm_users').toString().split(',');
		gs.log('count is ' + usersFromListCollector.length);
		for(var i = 0 ; i < usersFromListCollector.length ; i ++){
			var grUser = new GlideRecord('sys_user');
			grUser.addQuery('sys_id','IN',usersFromListCollector[i]);
			grUser.query();
			while(grUser.next()) { 

				obj.push(grUser.name + ' [' + grUser.user_name + ']');

			}

		}
		return obj.join(',');

	},


	type: 'CommTes'
});

 

Quick Demo

 

find_real_file.png

 

Note: Make the changes in script based on your need. 

 

If my answer helped you, kindly mark it as correct and helpful. 

Community Alums
Not applicable

I have this as the client script

find_real_file.png

and this as the SI: 

find_real_file.png

but when I submit the form I get javascript errors and no alerts. 

find_real_file.png

Please update your script , if you see my script i have done all the split() and loop part in script include. 

would it be possible to connect over zoom or webex ? i can quickly help you and fix this. 

 

if yes, drop me the details on email below. 

 

hvrdhn88@gmail.com

Community Alums
Not applicable

I very much appreciate the offer but it's a client instance that I can't share. GUESS WHAT, though? I spelled the name of my SI incorrectly when I called it from the client script, haha! I now get an alert with the right information. Thank you so much for all of your help!!