Copy values from string field to list collector

ceraulo
Mega Guru

Hello!

 

I have a catalog item that contains 2 variables of type Single Line Text and List Collector. The single line text contains a comma separated of User IDs. Is there a way to copy these user ids to a List Collector (List table: sys_user ) and display the Name?

 

Please help!

 

Thank you.

1 ACCEPTED SOLUTION

@ceraulo 

what you want to do by returning true or false?

If you want to set the values then do this

Script Include:

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

	getManagersList: function() {
		var user = this.getParameter('sysparm_value');
		var ans = [];
		var gr = new GlideRecord("sys_user");
		gr.addQuery("user_name", "IN", user);
		gr.query();
		while (gr.next()) {
			ans.push(gr.getUniqueValue());
		}
		return ans.toString();
	},

	type: 'UserDetailsUtils'
});

Client Script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '')
		return;

	var gx = new GlideAjax('UserUtils');
	gx.addParam('sysparm_name', 'getManagersList');
	gx.addParam('sysparm_value', g_form.getValue('managers_list_userid')); //text variable containing user ids
	gx.getXMLAnswer(function(answer){
		g_form.setValue('managers_list', answer.toString()); //list collector
	});

}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

9 REPLIES 9

I'm not sure if I would recommend doing it that way, at least if I now understand your requirement correctly, that would mean that you would need a client script, which loops through a list of usernames.

With these usernames use GlideAjax to lookup the sys_id of the users, and populate these in the list collector (which would then display the name).

That would potentially be a lot of processing on the form itself, which may create a very poor performance for the user filling the form.

However if you need to do that, you could follow those steps.


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.

@Peter Bodelier I have taken a look at the database and the list would only contain 1-3 user names. I made an attempt to create the catalog client script and script include but I could not get it to work.

 

Catalog Client Script

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '')
        return;

    var gx = new GlideAjax('UserUtils');
    gx.addParam('sysparm_name', 'getManagersList');
    gx.addParam('sysparm_value', g_form.getValue('managers_list_userid')); //text variable containing user ids
    gx.getXMLAnswer(ManagersListUserID);
}

function ManagersListUserID(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
    if (ga.getAnswer() != 'false') {
        g_form.setValue('managers_list', users); //list collector

    }
}

 

 

Script Include

 

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

        getManagersList: function() {

        var user = this.getParameter('sysparm_value');
        var splt = user.split(',');
        var ans = [];
		
        for (i = 0; i < splt.length; i++) {
            var gr = new GlideRecord("sys_user");
            gr.addQuery("user_name", splt[i]);
            gr.query();
            if (gr.next()) {
                ans.push(gr.getUniqueValue());
            }
        }
        if (ans.length > 0) {
            return ans;
        } else {
            return false;
        }
    },



    type: 'UserDetailsUtils'
});

 

 

Please help!

 

Thank you.

@ceraulo 

what you want to do by returning true or false?

If you want to set the values then do this

Script Include:

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

	getManagersList: function() {
		var user = this.getParameter('sysparm_value');
		var ans = [];
		var gr = new GlideRecord("sys_user");
		gr.addQuery("user_name", "IN", user);
		gr.query();
		while (gr.next()) {
			ans.push(gr.getUniqueValue());
		}
		return ans.toString();
	},

	type: 'UserDetailsUtils'
});

Client Script

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	if (isLoading || newValue === '')
		return;

	var gx = new GlideAjax('UserUtils');
	gx.addParam('sysparm_name', 'getManagersList');
	gx.addParam('sysparm_value', g_form.getValue('managers_list_userid')); //text variable containing user ids
	gx.getXMLAnswer(function(answer){
		g_form.setValue('managers_list', answer.toString()); //list collector
	});

}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Ankur Bawiskar
Tera Patron
Tera Patron

@ceraulo 

Do you expect your end users to remember the user IDs and enter those correctly in text variable?

If you still want this. you can use onChange catalog client script on that text variable and set the list collector by using GlideAjax call

1) make GlideAjax call and pass the comma separated values

2) query sys_user and store those records sysId into array

3) return that array and set the array in list collector

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Hmbergph1998
Tera Contributor

😊