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

Hi @Shakeel Shaik  I have same scenario but need to make that filed read only with that aupopulated list how should I achive that ? Can u please guide .

Shakeel Shaik
Giga Sage
Giga Sage

Demo:-

I have 4 users whose manager is Abel Tuter.

ShakeelShaik_1-1669542491078.png

ShakeelShaik_2-1669542894318.gif

 

 

Thanks,

Shakeel Shaik 🙂

 

Thanks,
Shakeel Shaik 🙂

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.

Hello,

 

If  my answer helped you can you also mark it as correct.

 

Thanks.