How to Test a Client Script That Disables Category Field for Non-ITIL Users on Incident Form

vaishali231
Tera Contributor

Hi everyone! 👋

I'm working on a client-side requirement for the Incident form, where the Category field should be disabled if the currently logged-in user does not have the itil role. To implement this, I used a Script Include + Client Script with GlideAjax.

 

Script Include

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

userrole: function () {
return gs.hasRole('itil') ? 'true' : 'false';
},

type: 'checkUserRole'
});

 

 

Client Script (Type: onLoad)

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

userrole: function () {
return gs.hasRole('itil') ? 'true' : 'false';
},

type: 'checkUserRole'
});

 

 

How I Tested:

  1. Impersonated a user with the itil roleCategory was editable 

  2. Impersonated a user without the itil roleCategory was disabled 

4 REPLIES 4

Arun_Manoj
Mega Sage

Hi  @vaishali231 ,

 

 

1. Script Include

Make sure this is set to Client Callable = true.

 

var checkUserRole = Class.create();
checkUserRole.prototype = Object.extendsObject(AbstractAjaxProcessor, {
userHasITILRole: function () {
return gs.hasRole('itil') ? 'true' : 'false';
},

type: 'checkUserRole'
});

2. Client Script (Type: onLoad)

(function executeRule(current, gForm, gUser, gSNC) {
var ga = new GlideAjax('checkUserRole');
ga.addParam('sysparm_name', 'userHasITILRole');
ga.getXMLAnswer(function(response) {
if (response === 'false') {
gForm.setDisabled('category', true);
}
});
})
 

How to Test

  1. Impersonate a user with itil role → Category should remain editable.

  2. Impersonate a user without itil role → Category should become disabled.

 

 

"How to Test a Client Script That Disables Category Field for Non-ITIL Users on Incident Form"​

 

How to test:

 
1) Find two users (one itil, one non-itil) - impersonate for each - spot the difference
2) Create dummy users, assign itil to just one of them, give them password that you can remember, go to /login.do and log in with their accounts in incognito / different browser.
 
Just a note - isn't ITIL a prerequisite to access an incident form?
What persona or who is the non-ITIL that is viewing Incidents?
  • is it itil_admin, admin, .... ???
———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


@vaishali231 
if you are testing visibility on incident for itil - then itil is the "lowest" that shall be able to access an incident.

So if your conditions are based on have itil or not, all the non-itil roles that can access incident will be "higher" than itil and usually it inherits itil. 

 

The condition shall be a little bit different unless this is a customised scenarion in your organisation.

 

Can you please review and let me know?

 

PS: when it comes to roles - be aware of (1) hasRole('itil') and (2) hasRoleExactly('itil').

(1) admin will return true, because itil is under admin

(2) user must have explicitly assigned itil, admin will return false

 

———
/* If my response wasn’t a total disaster ↙️ drop a Kudos or Accept as Solution ↘️ Cheers! */


Community Alums
Not applicable

But just to point out: in your message, you accidentally pasted the Script Include code twice and repeated it in the Client Script section.
In the Client Script, you don’t need to redefine the Script Include — instead, you call it from the client side using GlideAjax.

 

Here’s what your Client Script should look like (type: onLoad)

 function onLoad() {
  var ga = new GlideAjax('usercheckdata');
  ga.addParam('sysparm_name', 'userrole');
  ga.getXMLAnswer(function(response) {
    var hasRole = response;
    if (hasRole !== 'true') {
      g_form.setDisabled('category', true);
    }
  });
}

 

here is how you can test this 

a) Open the Incident form as your normal admin user → the Category field should stay editable.
b) Use Impersonate:

  • Impersonate a user who has the itil role → open the Incident form → Category should still be editable.

  • Impersonate a user without the itil role → open the Incident form → Category field should now be disabled (greyed out).