multiple auto-populate

laurawolter
Kilo Contributor

Hi all,

 

I have made a catalog item (or at least doing my best). I just received access to catalog builder.

Running into a challenge: I have one field with auto-populate. But I would like it to have multiple instead of just 1. 

Field : "requested for" is now single option only. 1 name. 

Field: "User ID" will be automatic populate with the correct information, the ID of the name in the field "requested for"

 

My goal/idea is that "requested for" can have multiple names and the field "user Id will populate the ID's automatically .

Is this even possible? If yes, how can I do this?

 

Kind regard,

Laura Wolter

1 ACCEPTED SOLUTION

Aranya
Tera Guru

Hi Laura,
For multiple names in one field, we have a field type called "List Collector". Set the field type to List Collector and set the table of the field as User(sys_user). See the photo attached below.
Write an onChange catalog client script on the Requested For field as follows:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	var ids = new GlideAjax('getRequestedUserIds');
	ids.addParam('sysparm_name','getUID');
	ids.addParam('sysparm_dispnames',newValue);
    ids.getXMLAnswer(showIDs);
   
}
function showIDs(answer){
	var userids = "";
	if(g_form.getValue('user_id') == ""){
		userids = answer;
	}
	else{
		userids = g_form.getValue('user_id') + " , " + answer;
	} 
	g_form.setValue('user_id',userids);
}


Now write the Script Include as:

var getRequestedUserIds = Class.create();
getRequestedUserIds.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	getUID: function(){
		var names = this.getParameter('sysparm_dispnames');
		var name_array = names.split(",");

		var id = new GlideRecord('sys_user');
		id.addQuery('sys_id',name_array.slice(-1));
		id.query();
		if(id.next()){
			return id.user_name;
		}
	},
    type: 'getRequestedUserIds'
});

The client script will pull the requested for sys id every time it changes and feed it to the script include who will send back the user id to be fed into the User ID field on the catalog item.

Aranya_0-1724510321482.png

Thanks!
P.S. Please mark as Helpful if I was able to clear your doubt.

View solution in original post

1 REPLY 1

Aranya
Tera Guru

Hi Laura,
For multiple names in one field, we have a field type called "List Collector". Set the field type to List Collector and set the table of the field as User(sys_user). See the photo attached below.
Write an onChange catalog client script on the Requested For field as follows:

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	var ids = new GlideAjax('getRequestedUserIds');
	ids.addParam('sysparm_name','getUID');
	ids.addParam('sysparm_dispnames',newValue);
    ids.getXMLAnswer(showIDs);
   
}
function showIDs(answer){
	var userids = "";
	if(g_form.getValue('user_id') == ""){
		userids = answer;
	}
	else{
		userids = g_form.getValue('user_id') + " , " + answer;
	} 
	g_form.setValue('user_id',userids);
}


Now write the Script Include as:

var getRequestedUserIds = Class.create();
getRequestedUserIds.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	getUID: function(){
		var names = this.getParameter('sysparm_dispnames');
		var name_array = names.split(",");

		var id = new GlideRecord('sys_user');
		id.addQuery('sys_id',name_array.slice(-1));
		id.query();
		if(id.next()){
			return id.user_name;
		}
	},
    type: 'getRequestedUserIds'
});

The client script will pull the requested for sys id every time it changes and feed it to the script include who will send back the user id to be fed into the User ID field on the catalog item.

Aranya_0-1724510321482.png

Thanks!
P.S. Please mark as Helpful if I was able to clear your doubt.