List collector the users with and without ITIL role and move them from another List Collector i tried below script but it's not checking the ITIL role

mani55
Tera Contributor

we have three list collector varibles 

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

i used in below script so present it's not checking the users role and if i am selecting any users those coming no access varible so that's worng 

 

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'
});

 

Client script:

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

	var ga = new GlideAjax('getroles');
	ga.addParam('sysparm_name','isLicensed');
	ga.addParam('sysparm_user',newValue);
	ga.getXMLAnswer(response);
	
	function response(answer){
		try{
		if(answer == 'true'){
			addUser(newValue,"user_access");
		} else{
			addUser(newValue,"no_access");
		}
			//g_form.setValue('user_list','');
		}catch(e){

		}

	}
	
	function addUser(value,list){
		var vals = g_form.getValue(list);
		alert('the data'+vals);

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

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

Mahendra RC
Mega Sage

Hello Mani,

Please check with below scripts:

Client script:

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

	var ga = new GlideAjax('getroles');
	ga.addParam('sysparm_name','isLicensed');
	ga.addParam('sysparm_user',newValue);
	ga.getXMLAnswer(parseResponse);
	
	function parseResponse(answer){
		try{
		var usersData = JSON.parse(answer);
		if (usersData["itil_users"]){
		var accessUser = usersData["itil_users"];
		g_form.setValue("user_access", accessUser.join(","));
		}
		if (usersData["non_itil_users"]){
		var noAccessUser = usersData["non_itil_users"];
		g_form.setValue("no_access", noAccessUser.join(","));
		}
		}catch(e){
		}
	}
}

Script Include:

var CatUtils = Class.create();
CatUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	isLicensed: function(){
		var userRoles = {};
		var itilRoleUsers = [];
		var nonItilRoleUsers = [];
		var userGuid = this.getParameter('sysparm_user').split(",");
		for (var users in userGuid) {
			var userRoleGR = new GlideRecord('sys_user_has_role');
			userRoleGR.addQuery('user' , userGuid[users]);
			userRoleGR.addQuery('role', '282bf1fac6112285017366cb5f867469'); //ITIL Role
			userRoleGR.query();
			if(!userRoleGR.hasNext()){
				nonItilRoleUsers.push(userGuid[users]);
			} else {
				userRoleGR.next();
				itilRoleUsers.push(userRoleGR.getValue("user"));
			}
		}		
		if (nonItilRoleUsers.length)
			userRoles["non_itil_users"] = nonItilRoleUsers;
		if (itilRoleUsers.length)
			userRoles["itil_users"] = itilRoleUsers;
		return JSON.stringify(userRoles);
	},
	
    type: 'CatUtils'
});

Please mark my respsone as helpful/correct, if it answer your question.

Thanks

View solution in original post

4 REPLIES 4

Mark Manders
Mega Patron

What exactly is the requirement behind this? I see you are asking several questions on the same topic and it looks to be a very complex solution for something. Maybe there's an easier way (also for the future, when this needs to be maintained). Can you share the use case?

If my answer helped you in any way, please then mark it as helpful. If it resolved the issue, please mark it as correct. This way others will find it in the solved queue and helps them on similar queries.

Mark


Please mark any helpful or correct solutions as such. That helps others find their solutions.
Mark

Thanks for ur response please find below screen shot below 3 variables are list collector variables

if user have ITIL role that user we need display "user access" varible.

if user don't have ITIL role that user we need display "No access" variable.

 

find_real_file.png

Mahendra RC
Mega Sage

Hello Mani,

Please check with below scripts:

Client script:

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

	var ga = new GlideAjax('getroles');
	ga.addParam('sysparm_name','isLicensed');
	ga.addParam('sysparm_user',newValue);
	ga.getXMLAnswer(parseResponse);
	
	function parseResponse(answer){
		try{
		var usersData = JSON.parse(answer);
		if (usersData["itil_users"]){
		var accessUser = usersData["itil_users"];
		g_form.setValue("user_access", accessUser.join(","));
		}
		if (usersData["non_itil_users"]){
		var noAccessUser = usersData["non_itil_users"];
		g_form.setValue("no_access", noAccessUser.join(","));
		}
		}catch(e){
		}
	}
}

Script Include:

var CatUtils = Class.create();
CatUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
	
	isLicensed: function(){
		var userRoles = {};
		var itilRoleUsers = [];
		var nonItilRoleUsers = [];
		var userGuid = this.getParameter('sysparm_user').split(",");
		for (var users in userGuid) {
			var userRoleGR = new GlideRecord('sys_user_has_role');
			userRoleGR.addQuery('user' , userGuid[users]);
			userRoleGR.addQuery('role', '282bf1fac6112285017366cb5f867469'); //ITIL Role
			userRoleGR.query();
			if(!userRoleGR.hasNext()){
				nonItilRoleUsers.push(userGuid[users]);
			} else {
				userRoleGR.next();
				itilRoleUsers.push(userRoleGR.getValue("user"));
			}
		}		
		if (nonItilRoleUsers.length)
			userRoles["non_itil_users"] = nonItilRoleUsers;
		if (itilRoleUsers.length)
			userRoles["itil_users"] = itilRoleUsers;
		return JSON.stringify(userRoles);
	},
	
    type: 'CatUtils'
});

Please mark my respsone as helpful/correct, if it answer your question.

Thanks

Hello @ Mani  

Just wanted to check with you, if the above response answered your question. If yes, then please do close this thread/question by marking the appropriate response as correct.

Thanks