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

RAMANA MURTHY G
Mega Sage
Mega Sage

Hello @Puneet4418 ,

You can achieve this using Flow Designer too

RAMANAMURTHYG_2-1694770464075.png

 

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

G Ramana Murthy
ServiceNow Developer