how to provide role to user by checkbox.

Puneet4418
Tera Contributor

Hi,

 

I have a requirement where we want to add/remove a particular role from user profile when a Checkbox "xyzRole" available in the custom application form is checked/ unchecked.

1 ACCEPTED SOLUTION

RAMANA MURTHY G
Mega Sage
Mega Sage

Hello @Puneet4418 ,

Try Insert/Update  - After Business rule on a User table, You said, you have a custom check box for xyzRole. So try below code (you can modify code according to your requirement)

 

 

(function executeRule(current, previous /*null when async*/) {

	var roleValue = current.u_demo_test_role;    // back end name of custom check box of xyzRole
	// gs.addInfoMessage(roleValue);
	if(roleValue == true){
		var addRole = new GlideRecord('sys_user_has_role');
		addRole.initialize();
		addRole.user = current.sys_id;
		addRole.role = '1fff282147dd71109f1d374a436d432b';   // sys_id of role, which is given in check box
		addRole.insert();
		// gs.addInfoMessage('Role inserted');
	}
	else if(roleValue == false){
		var removeRole = new GlideRecord('sys_user_has_role');
		removeRole.addEncodedQuery("user="+current.sys_id+"^role=1fff282147dd71109f1d374a436d432b");   // sys_id of role, which is given in check box
		removeRole.query();
		if(removeRole.next()){
			removeRole.deleteRecord();
		}
		// gs.addInfoMessage('Role Removed');
	}

})(current, previous);

 

  

Please mark my answer as helpful, if it helps you

Thank you

Please mark my answer helpful  & correct if it helps you
Thank you

G Ramana Murthy
ServiceNow Developer

View solution in original post

5 REPLIES 5

Saurabh Gupta
Kilo Patron
Kilo Patron

Hi,

You can write a business rule to achieve the same.

 

 


Thanks and Regards,

Saurabh Gupta

Ankur Bawiskar
Tera Patron
Tera Patron

@Puneet4418 

you can use business rule or flow designer for this

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

RAMANA MURTHY G
Mega Sage
Mega Sage

Hello @Puneet4418 ,

Try Insert/Update  - After Business rule on a User table, You said, you have a custom check box for xyzRole. So try below code (you can modify code according to your requirement)

 

 

(function executeRule(current, previous /*null when async*/) {

	var roleValue = current.u_demo_test_role;    // back end name of custom check box of xyzRole
	// gs.addInfoMessage(roleValue);
	if(roleValue == true){
		var addRole = new GlideRecord('sys_user_has_role');
		addRole.initialize();
		addRole.user = current.sys_id;
		addRole.role = '1fff282147dd71109f1d374a436d432b';   // sys_id of role, which is given in check box
		addRole.insert();
		// gs.addInfoMessage('Role inserted');
	}
	else if(roleValue == false){
		var removeRole = new GlideRecord('sys_user_has_role');
		removeRole.addEncodedQuery("user="+current.sys_id+"^role=1fff282147dd71109f1d374a436d432b");   // sys_id of role, which is given in check box
		removeRole.query();
		if(removeRole.next()){
			removeRole.deleteRecord();
		}
		// gs.addInfoMessage('Role Removed');
	}

})(current, previous);

 

  

Please mark my answer as helpful, if it helps you

Thank you

Please mark my answer helpful  & correct if it helps you
Thank you

G Ramana Murthy
ServiceNow Developer

Vedang1
Tera Expert

The simplest and way would be to create a flow designer on trigger of that field change, which in turn adds the Role to the user. 

Please mark it as solution proposed and helpful if its serves your purpose.

Thank you,
Vedang