Restrict values in a reference field based on user role

Robin Chadwick1
Giga Expert

We have the Service field on our incident form.  One of the services in the table is "SAP".  I want to restrict the selection of SAP as a business service in an incident to only those users that have the role "sap_authorized_user" (this is a new role we are creating).

How do I filter the values in the Service field so that SAP cannot be chosen if the user doesn't have the specified role.

1 ACCEPTED SOLUTION

Finalized the working script.

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

checkroleof: function(openbyis) {
var servicesare = '';
var reqbyuseris = gs.getUserID();
var rolenameis = 'sap_authorized_user';
var chkuserrole = new GlideRecord('sys_user_has_role');
chkuserrole.addQuery('user', reqbyuseris);
chkuserrole.addQuery('role.name', 'sap_authorized_user');
chkuserrole.query();

if (chkuserrole.next()) {
servicesare = 'nameLIKEsap^ORnameANYTHING';
}
else {
servicesare = 'nameNOT LIKEsap';
}
return servicesare;
},

type: 'getSAPService'
});

View solution in original post

9 REPLIES 9

I must be doing something wrong. 

I have over ridden the reference qualifier with the qualifier you provided - javascript: new getSAPService().checkroleof(current.opened_by);

 

I have created the script include as you specified.  

I have 2 users I'm using to test

  • Joe Employee (ITIL) - does not have the sap_authorized_user role.
  • Joe Employee (SAP) - has the sap_authorized_user role.

When I impersonate Joe Employee (ITIL) and create a new incident, I am still able to select any of the SAP services, as I can when impersonating Joe Employee (SAP).

Was there a part of your sample code that I needed to change for my purposes?

I figured it out. 

In the variable declaration - var reqbyuseris = openedbyis;

The value openedbyis need to be "current.openedby"

Thanks for your help.

I don't see where I can select correct.

Finalized the working script.

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

checkroleof: function(openbyis) {
var servicesare = '';
var reqbyuseris = gs.getUserID();
var rolenameis = 'sap_authorized_user';
var chkuserrole = new GlideRecord('sys_user_has_role');
chkuserrole.addQuery('user', reqbyuseris);
chkuserrole.addQuery('role.name', 'sap_authorized_user');
chkuserrole.query();

if (chkuserrole.next()) {
servicesare = 'nameLIKEsap^ORnameANYTHING';
}
else {
servicesare = 'nameNOT LIKEsap';
}
return servicesare;
},

type: 'getSAPService'
});

Robin Chadwick1
Giga Expert

If possible, I would appreciate some example Java Script that I can work from.

Thanks in advance.