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

Hi Ankur,

Again thanks for the feedback, Turns out, the earlier scripts shared is correct for OnChange, i tested in my Personal instance and it is working, i found and disabled the script causing it not to set in our Client instance. It is working now OnChange 🙂 Though i am still looking if we can make it to OnSubmit.

Hi,

setting the value in onSubmit would be difficult since you can only use Asynchronous GlideAjax in portal and by the time the value is set the form may submit.

Regards
Ankur

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

Hi Kieran,

Thanks for sharing this. I just tested in my Personal instance and it is working, i found and disabled the script causing it not to set in our Client instance. It is working now OnChange 🙂

By any chance, do you know how to make this OnSubmit? List Collector 2 and 3 are supposed to be hidden (to be used in WF approval, notif etc) and only the List Collector 1 should be exposed in the form so users can select all the members at once. I appreciate you for looking into this.

 

As pointed out by Ankur, this won't be achievable onSubmit as synchronous isn't supported.

One way is to add a script into your workflow to transform the values and them into two seperate scratchpad values (removing the need for the additional variables) or you could present the variables like I did on the form but make them read only to the user.