Identify from a List collector the users with and without ITIL role and move them from another List Collector

Gracie2
Kilo Contributor

Hi,

In our Group Creation SC form,  we have a "Group Member" List Collector1 variable(variable attribute: glide_list reference: user table) where user manually select the users from the list. OnSubmit or Onchange of Catalog, we need to identify which of these users already has an existing role and  move those users with no roles to another List Collector2 with same attribute as the first (variable attribute: glide_list reference: user table) to separate them and to make the referencing easier in the Workflow approval process.

List Collector  1 Variable - users input all members (with and without role)

List Collector 2 (Hidden in the form)- This needs to populate with users from List Collector 1 with no ITIL role

List Collector 3 (Hidden in the form)- This needs to populate with users from List Collector 1 with ITIL role

 

find_real_file.png

 

I initially created an Onchange Client Script and Script include for same requirement before to identify if user already has an ITIL role but field is only a reference and no population required, List collectors seem to be complex.

Any help will be very much appreciated. Thank you!

 

 

1 ACCEPTED SOLUTION

Kieran Anson
Kilo Patron

Hi This can be done if you need to split the users into individual variables. If you're using a dedicated workflow for this item, you could just use a script action that runs through the variable and itterates over the values and then stores the values in two arrays on the workflow scratchpad?

 

Using an onChange script will be invoked every time a user is added. 

var CatUtils = Class.create();
CatUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	isLicensed: function(){
		var answer = false;
		var user = new GlideRecord('sys_user_has_role');
		user.addQuery('user' , this.getParameter('sysparm_user'));
		user.addQuery('role', '282bf1fac6112285017366cb5f867469'); //ITIL Role
		user.query();
		if(user.hasNext()){
			answer = true;
		}
		
		return answer;
	},
	
    type: 'CatUtils'
});

Client Script:

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

	var ga = new GlideAjax('CatUtils');
	ga.addParam('sysparm_name','isLicensed');
	ga.addParam('sysparm_user',newValue);
	ga.getXMLAnswer(response);
	
	function response(answer){
		try{
		if(answer == 'true'){
			addUser(newValue,"license");
		} else{
			addUser(newValue,"unlicense");
		}
		
		g_form.setValue('users','');
		}catch(e){

		}

	}
	
	function addUser(value,list){
		var vals = g_form.getValue(list);

		if(vals.indexOf(",") > -1 || vals != ''){

			vals += "," + value
		} else{
			vals = value;
		}
		g_form.setValue(list,vals);
	}
}

find_real_file.png

View solution in original post

13 REPLIES 13

Kieran Anson
Kilo Patron

Hi This can be done if you need to split the users into individual variables. If you're using a dedicated workflow for this item, you could just use a script action that runs through the variable and itterates over the values and then stores the values in two arrays on the workflow scratchpad?

 

Using an onChange script will be invoked every time a user is added. 

var CatUtils = Class.create();
CatUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	isLicensed: function(){
		var answer = false;
		var user = new GlideRecord('sys_user_has_role');
		user.addQuery('user' , this.getParameter('sysparm_user'));
		user.addQuery('role', '282bf1fac6112285017366cb5f867469'); //ITIL Role
		user.query();
		if(user.hasNext()){
			answer = true;
		}
		
		return answer;
	},
	
    type: 'CatUtils'
});

Client Script:

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

	var ga = new GlideAjax('CatUtils');
	ga.addParam('sysparm_name','isLicensed');
	ga.addParam('sysparm_user',newValue);
	ga.getXMLAnswer(response);
	
	function response(answer){
		try{
		if(answer == 'true'){
			addUser(newValue,"license");
		} else{
			addUser(newValue,"unlicense");
		}
		
		g_form.setValue('users','');
		}catch(e){

		}

	}
	
	function addUser(value,list){
		var vals = g_form.getValue(list);

		if(vals.indexOf(",") > -1 || vals != ''){

			vals += "," + value
		} else{
			vals = value;
		}
		g_form.setValue(list,vals);
	}
}

find_real_file.png

Thanks for this script, Kieran. Yes, we plan to use the values stored in the 2 variables to make it easier in the WF approval process.

I tried this in our instance but the license/unlicense list doesn't seem populating onchange. 😞 Can you help me check below please? Also, do you think this will run if multiple users are selected at once? or is Onsubmit better?

find_real_file.png

 

find_real_file.png

 

Onchange client Script on users variable

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('CatUtils');
ga.addParam('sysparm_name','isLicensed');
ga.addParam('sysparm_user',newValue);
ga.getXMLAnswer(response);

function response(answer){
try{
if(answer == 'true'){
addUser(newValue,"license");
} else{
addUser(newValue,"unlicense");
}

g_form.setValue('users','');
}catch(e){

}

}

function addUser(value,list){
var vals = g_form.getValue(list);

if(vals.indexOf(",") > -1 || vals != ''){

vals += "," + value;
} else{
vals = value;
}
g_form.setValue(list,vals);

}

}

 

 

Script Include

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

isLicensed: function(){
var answer = false;
var user = new GlideRecord('sys_user_has_role');
user.addQuery('user' , this.getParameter('sysparm_user'));
user.addQuery('role', '282bf1fac6112285017366cb5f867469'); //ITIL Role
user.query();
if(user.hasNext()){
answer = true;
}

return answer;
},


type: 'CatUtils'
});

Hi,

update as below

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('CatUtils');
ga.addParam('sysparm_name','isLicensed');
ga.addParam('sysparm_user',newValue);
ga.getXMLAnswer(response);

function response(answer){
try{
if(answer.toString() == 'true'){
addUser(newValue,"license");
} else{
addUser(newValue,"unlicense");
}

g_form.setValue('users','');
}catch(e){

}

}

function addUser(value,list){
var vals = g_form.getValue(list);

if(vals.indexOf(",") > -1 || vals != ''){

vals += "," + value;
} else{
vals = value;
}
g_form.setValue(list,vals.toString());

}

}

Regards
Ankur

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

Hi Ankur, 

Thanks for replying. This doesn't seem to work still. License/Unlicense list not populated when 'users' variable is populated. Any chance you can convert this to OnSubmit one? That way user can multiple select at once? I appreciate your time helping me.

 

find_real_file.png