Restrict the close of the catalog task in servicenow

vinuth v
Tera Expert

Hi All,

 

I need to restrict the closing of the catalog task from the user.

Negative scenarios

1. If the user is not part of the catalog task assignment group, they can't able to close the task

2. If the user is "requested for" then he can't able to close the task.

 

Positive scenarios

1. If the person who raised the request he can close the catalog task

2. If the person belong to the particular assignment group then he can close the catalog task.

3. If the person belong to the "Authorize-ITSM" group then he can close the catalog task.

 

I have written the onload client script

 

function onLoad() {

 

var groupId= g_form.getValue('assignment_group');
var req= g_form.getValue('u_requested_by')
var get = new GlideAjax('CheckUserMemberr');
    get.addParam('sysparm_name', 'checkUser');
    get.addParam('sysparm_group', groupId);
get.addParam('sysparm_req', g_user.userID);
 get.getXML(addOpts);
function addOpts(resp) {
var answer = resp.responseXML.documentElement.getAttribute("answer");
if((g_user.hasRole('itil') && answer=='true') || g_user.hasRole('admin') || req == g_user.userID){
g_form.setReadOnly('state', false);
}
else{
g_form.setReadOnly('state', true);
}
}
 
 
Script Include
var CheckUserMemberr = Class.create();
CheckUserMemberr.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkUser: function() {
var a = 'false';
 var group = this.getParameter('sysparm_group');
var requestor = this.getParameter('sysparm_req');
 
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group.sys_id',group);
//gr.addEncodedQuery('group.sys_id'+ group + 'ORgroup.sys_id=fd40e2c0130b3e80a6b576d66144b0ac');
this part is working fine in one of the instance if try to do it in another instance it is not working as expected.
gr.addQuery('user.sys_id',requestor);
gr.query();
if(gr.next()){
a= 'true';
}
return a;
},
    type: 'CheckUserMemberr'
});
 
Query:
If I uncomment this  gr.addQuery('group.sys_id',group); part it is working fine for admin, user is belog to the particular group and user raised the request. I wanted to add one more condition along with the above code that is "If the user is member of the "Authorize-ITSM" group. for this I tried with the below code instead of 
 gr.addQuery('group.sys_id',group);   to
gr.addEncodedQuery('group.sys_id'+ group + 'ORgroup.sys_id=fd40e2c0130b3e80a6b576d66144b0ac');
 
But it is working in one of the instance and not working in other instance in servicenow.
 
Please any one provide me the input,
 
Thanks in advance,
Vinuth
3 REPLIES 3

Punit S
Giga Guru

Based on your description, you want to restrict the closing of the catalog task based on certain conditions. To achieve this, you can modify your existing script to include the additional condition of checking if the user is a member of the "Authorize-ITSM" group. Here's an updated version of the script:

OnLoad Client Script:

function onLoad() {
  var groupId = g_form.getValue('assignment_group');
  var req = g_form.getValue('u_requested_by');

  var get = new GlideAjax('CheckUserMember');
  get.addParam('sysparm_name', 'checkUser');
  get.addParam('sysparm_group', groupId);
  get.addParam('sysparm_req', g_user.userID);
  get.getXML(addOpts);

  function addOpts(resp) {
    var answer = resp.responseXML.documentElement.getAttribute('answer');
    if (
      (g_user.hasRole('itil') && answer === 'true') ||
      g_user.hasRole('admin') ||
      req === g_user.userID ||
      g_user.isMemberOf('Authorize-ITSM')
    ) {
      g_form.setReadOnly('state', false);
    } else {
      g_form.setReadOnly('state', true);
    }
  }
}

 

Script include 

 

var CheckUserMember = Class.create();
CheckUserMember.prototype = Object.extendsObject(AbstractAjaxProcessor, {
  checkUser: function() {
    var isMember = 'false';
    var group = this.getParameter('sysparm_group');
    var requestor = this.getParameter('sysparm_req');

    var gr = new GlideRecord('sys_user_grmember');
    gr.addQuery('group.sys_id', group);
    gr.addQuery('user.sys_id', requestor);
    gr.query();

    if (gr.next()) {
      isMember = 'true';
    }

    return isMember;
  },
  type: 'CheckUserMember'
});

In this updated script, the condition g_user.isMemberOf('Authorize-ITSM') is added to check if the current user is a member of the "Authorize-ITSM" group. This condition is included in the if statement to determine if the user can close the catalog task.

Please ensure that the sys_id provided for the "Authorize-ITSM" group is correct and matches the group's ID in your instance. Additionally, ensure that the Script Include is correctly referenced in the onLoad Client Script and that the Script Include itself is correctly named.

After implementing these changes, the catalog task closure should be restricted based on the specified conditions.

 

Please mark my answer as a solution/helpful in case it adds value and moves you a step closer to your desired ServiceNow solution goal.

Thanks,
Punit

Hi @Punit S 

 

I tried with the above script but it is not working as expected.

 

Thanks,

Vinutha

vinuth v
Tera Expert

Hi All,

 

The below script is working fine, except one condition like "ITIL" user can edit the task state.

 

I have written the onload client script

 

function onLoad() {

 

var groupId= g_form.getValue('assignment_group');
var req= g_form.getValue('u_requested_by')
var get = new GlideAjax('CheckUserMemberr');
    get.addParam('sysparm_name', 'checkUser');
    get.addParam('sysparm_group', groupId);
get.addParam('sysparm_req', g_user.userID);
 get.getXML(addOpts);
function addOpts(resp) {
var answer = resp.responseXML.documentElement.getAttribute("answer");
if((g_user.hasRole('itil') && answer=='true') || g_user.hasRole('admin') || req == g_user.userID){
g_form.setReadOnly('state', false);
}
else{
g_form.setReadOnly('state', true);
}
}
 
 
Script Include
var CheckUserMemberr = Class.create();
CheckUserMemberr.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkUser: function() {
var a = 'false';
 var group = this.getParameter('sysparm_group');
var requestor = this.getParameter('sysparm_req');
 
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('group.sys_id',group);
//gr.addEncodedQuery('group.sys_id'+ group + 'ORgroup.sys_id=fd40e2c0130b3e80a6b576d66144b0ac');
this part is working fine in one of the instance if try to do it in another instance it is not working as expected.
gr.addQuery('user.sys_id',requestor);
gr.query();
if(gr.next()){
a= 'true';
}
return a;
},
    type: 'CheckUserMemberr'
});
 
But "ITIL" user can edit the task, if the itil user is not part of the specific group and he is not the requested for and he is not belong to the "Authorize-ITSM" group.
 
Please any one provide me the input,
 
Thanks in advance,
Vinuth