Restrict users to reject a PRB except one group

Hervi
Tera Contributor

Hi,

Requirement - only users of a particular group should be able to reject a PRB.

 

Script Include - 

 

 

var CheckUserGroup = Class.create();
CheckUserGroup.prototype = {
   initialize: function() {
   },
   checkMembership: function(userId, groupName) {
       var gr = new GlideRecord('sys_user_grmember');
       gr.addQuery('user', userId);
       gr.addQuery('group.name', groupName);
       gr.query();
       return gr.hasNext().toString();
   },
   type: 'CheckUserGroup'
};

 

 

Client Script -

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
       return;
   }
   if (newValue == 9) {
       var userId = g_user.userID;
       var ga = new GlideAjax('CheckUserGroup');
       ga.addParam('sys_id', userId);
       ga.addParam('group_name', 'Incident and Problem Managers'); 
       ga.addParam('sysparm_name', 'checkMembership');
       ga.getXMLAnswer(function(response) {
           var answer = response.responseXML.documentElement.getAttribute("answer");
           if (answer == 'false') {
               g_form.setValue('state', oldValue);
               g_form.addErrorMessage('You are not authorized to reject this problem.');
           }
       });
   }
}

 

 I tried above codes but not working. Any thoughts?

 

1 REPLY 1

Bhavya11
Kilo Patron

Hi @Hervi 

 

Few issues are in your code

1.whenever your calling the Script include in Client side then mark the Script include  as Client Callable true

2. few changes need to me made on On-change client script like (please follow this for more information https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2...)

   

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
       return;
   }
   if (newValue == 9) {
       var userId = g_user.userID;
       var ga = new GlideAjax('CheckUserGroup');
      ga.addParam('sysparm_name', 'checkMembership');
       ga.addParam('sysparm_sysid', userId);
       ga.addParam('sysparm_groupname', 'Incident and Problem Managers'); // instead of this you can stored it in property can be called at the Script include 
       ga.getXMLAnswer(function(response) {
           var answer = response;
           if (answer == 'false') {
               g_form.setValue('state', oldValue);
               g_form.addErrorMessage('You are not authorized to reject this problem.');
           }
       });
   }
}

 

3. changes on script include be like

 

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

checkMembership: function() {
		var userId=this.getParameter('sysparm_sysid');
		var groupName=this.getParameter('sysparm_groupname');
       var gr = new GlideRecord('sys_user_grmember');
       gr.addQuery('user', userId);
       gr.addQuery('group.name', groupName);
       gr.query();
       return gr.hasNext();
   },

    type: 'CheckUserGroup'
});

 

 If my response proves useful, please indicate its helpfulness by selecting "Accept as Solution" and " Helpful."

 

Thanks,

BK