Autopopulate a list collector of active users whose manager is in the manager field.

Le_Bear
Tera Contributor

Hello everyone.

 

I want to autopopulate a list collector (variable name: initial_members) of active users whose manager is in the manager field. 

 

Le_Bear_0-1669540022956.png

I am new to servicenow and I don't have much knowledge about scripting so I highly appreciate your help.

2 ACCEPTED SOLUTIONS

Shakeel Shaik
Giga Sage
Giga Sage

Hi @Le_Bear 

ShakeelShaik_0-1669542214872.png

 

Open your Initial Members variable and go to the Type Specification Tab as shown in the above image.
here the backend value of Manager is manager. Make the necessary changes and add the query in Reference Qualifier.

Thanks,
Shakeel Shaik 🙂

Thanks,
Shakeel Shaik 🙂

View solution in original post

Saurav11
Kilo Patron
Kilo Patron

Hello,

 

You can use the below code to auto populate user based on the manager field selected:-

 

Onchnage Client script on manager field. Just replace your list collector field name:-

 

 

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

   // give here the list collector variable name
	g_form.clearValue('replacelistcollectorfieldname');

	

	var ajax = new GlideAjax('getAssignGroups');
	ajax.addParam('sysparm_name', 'getUserGroups');
	ajax.addParam('sysparm_request_for', newValue);
	ajax.getXML(populateValues);
}

function populateValues(response) {
	var answer = response.responseXML.documentElement.getAttribute("answer");
	

        // give here the list collector variable name
	
		g_form.setValue('replacelistcollectorfieldname', answer); // Mobile/Portal Compatible

	
}

 

 

Script include with name getAssignGroups and make sure client callabale chekcbox is true

 

 

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

	getUserGroups: function(){

		var listGroup = [];
		var requester = this.getParameter('sysparm_request_for');
		var parm = this.getParameter('sysparm_view');

		var jsonArr = [];

		var recGrp = new GlideRecord('sys_user');
		recGrp.addQuery('manager', requester);
		recGrp.query();
		while (recGrp.next()) {

			
				listGroup.push(recGrp.sys_id.toString());
			
		}

		
			return listGroup.toString();
		
		

	},

	type: 'getAssignGroups'
});

 

 

Now once you select the manager field the list collector will be autopopulated you don't have to select it manually.

 

Also if you want to learn more about it you can check the below article

 

https://www.servicenow.com/community/developer-blog/dynamically-set-list-collector-on-change-of-vari...

 

Please mark my answer as correct based on Impact.

View solution in original post

8 REPLIES 8

Shakeel Shaik
Giga Sage
Giga Sage

Hi @Le_Bear 

ShakeelShaik_0-1669542214872.png

 

Open your Initial Members variable and go to the Type Specification Tab as shown in the above image.
here the backend value of Manager is manager. Make the necessary changes and add the query in Reference Qualifier.

Thanks,
Shakeel Shaik 🙂

Thanks,
Shakeel Shaik 🙂

Thank you very much. It is an acceptable answer.

 

However, I am curious if it is possible to configure in a way so that it will auto select the populated users in the field instead of manually clicking the users. 

Le_Bear_0-1669542847524.png

 

Like so.

Hi Shakeel,

I have a question. What if I am basing my list collector on this reference field: 

image.png

 

So like if Asia or Western Europe is selected. It will only show the contents related to it

 
image.png
If Asia is selected, it will only show in the list collector: Training 100, Training 200.

Hi @MelvinBulan,

You can do it please refer to my above response or if you want the exact solution, then please share the backend values of Table and Fields.

Thanks,
Shakeel Shaik 🙂

Thanks,
Shakeel Shaik 🙂