Based on the requested for role(itil) hide the field in order guide

Community Alums
Not applicable

Hi All, 

 

Based on the selected requested_for user contain role (itil) hide field in order guide. 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var hasRole = g_user.hasRole('itil');
if (hasRole == true) {
g_form.setVisible('esm', true);
} else{
g_form.setVisible('esm', false);
}
}

 

is not working any suggestion to achieve the requirement. 

 

Thanks. 

 

4 REPLIES 4

Shruti Khaire
Kilo Sage

Hello @Community Alums,

 

I think "hasRole('whatever')" always returns true if you are the admin, no wonder if the role really exists or not.

Instead use g_user.hasRoleExactly('itil') which will resolve your issue.

Hope it helps!

Prashant Ahire
Kilo Sage

Hello @Community Alums 

Please check this link- 

https://www.servicenow.com/community/itsm-forum/how-to-check-the-requested-for-has-itil-role-or-not/m-p/752404 


Please check and Mark Correct and Helpful if it really helps you.
Regards,
Prashant Ahire

 

AnveshKumar M
Tera Sage
Tera Sage

Hi @Community Alums 

 

You said you need to do it based on the selected Requested for user role, but you are using g_form.hasRole(), this will check for the roles of the user who is filling the form. To Achieve this requirement you need to use ClientCallable Script Include and Glide Ajax in your onChange client script.

 

I'm assuming that you wrote your onChange catalog client script on the requested for variable.

 

Client Callable Script Include: Navigate to System Definition -> Script Includes and create a new record like the one below.

AnveshKumarM_0-1702289176160.png

 

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

	checkUserHasITIL: function(){
		var user = this.getParameter("sysparm_user");

		var roleGr = new GlideRecord("sys_user_has_role");
		roleGr.addEncodedQuery("role.name=itil^user=" + user);
		roleGr.query();
		if(roleGr._next()){
			return 'true';
		}
		return 'false';
	},

    type: 'CustomUserUtilsClient'
});

  

onChange Catalog Client Script: Change your catalog client script to the one below.

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
   var ga = new GlideAjax("CustomUserUtilsClient");
   ga.addParam("sysparm_name", "checkUserHasITIL");
   ga.addParam("sysparm_user", newValue);
   ga.getXMLAnswer(processResp);
   function processResp(hasRole){
      if (hasRole == 'true') {
         g_form.setVisible('esm', false);
      } else{
         g_form.setVisible('esm', true);
      }
   }
}

 

If your onChange client script is not on Requested for user field, use the following script.

 

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
   var user = g_form.getValue("YOUR_REQUESTED_FOR_VARIABLE_NAME");
   var ga = new GlideAjax("CustomUserUtilsClient");
   ga.addParam("sysparm_name", "checkUserHasITIL");
   ga.addParam("sysparm_user", newValue);
   ga.getXMLAnswer(processResp);
   function processResp(hasRole){
      if (hasRole == 'true') {
         g_form.setVisible('esm', false);
      } else{
         g_form.setVisible('esm', true);
      }
   }
}

 

If you want this to work on onLoad also, create a new onLoad client script with the following code.

 

function onLoad() {
   var user = g_form.getValue("YOUR_REQUESTED_FOR_VARIABLE_NAME");
   var ga = new GlideAjax("CustomUserUtilsClient");
   ga.addParam("sysparm_name", "checkUserHasITIL");
   ga.addParam("sysparm_user", newValue);
   ga.getXMLAnswer(processResp);
   function processResp(hasRole){
      if (hasRole == 'true') {
         g_form.setVisible('esm', false);
      } else{
         g_form.setVisible('esm', true);
      }
   }
}

 

Please mark my answer helpful and accept as a solution if it helped 👍✔️

Thanks,
Anvesh

Ankit Kumar6
Tera Contributor

Dear @Community Alums 

Check the below code it might help you

function onChange(control, oldValue, newValue, isLoading) {
    if (isLoading || newValue == '') {
        return;
    }
    var hasRole = g_user.hasRoleExactly('itil');
    if (hasRole == true) {
        g_form.setDisplay('esm', false);
    } else {
        g_form.setDisplay('esm', true);
    }
}
 
Thanks,
Ankit
If my answer helps you, please mark it helpful or correct.