Query business rule to restrict access

mynedisanjana
Tera Contributor

Hi ,

 

We have a requirement where we need to check the access based on the role that user is having and need to compare the role of the assignment group . if the user is having a role "A" and the incident assignment group is also having the role "A" then the logged in user need to able to see the records. can anyone let me know how we can achieve this using a query business rule?

2 REPLIES 2

Sai Shravan
Mega Sage

Hi @mynedisanjana ,

Here is the sample code you can give a try

(function executeRule(current, previous, /*null when async*/) {
  var userRoles = gs.getUser().getRoles(); // Get the roles of the currently logged-in user

  // Get the assignment group's roles
  var assignmentGroup = new GlideRecord('sys_user_group');
  if (assignmentGroup.get('sys_id', current.assignment_group)) {
    var assignmentGroupRoles = assignmentGroup.getValue('roles');

    // Check if the user has a role in common with the assignment group
    var hasCommonRole = false;
    for (var i = 0; i < userRoles.size(); i++) {
      if (assignmentGroupRoles.indexOf(userRoles.get(i)) !== -1) {
        hasCommonRole = true;
        break;
      }
    }

    // If there is no common role, restrict access to the incident
    if (!hasCommonRole) {
      current.setWorkflow(false); // Prevent saving the incident
      gs.addErrorMessage('You do not have the required access to this incident.');
    }
  }
})(current, previous);

when a user tries to insert or update an incident record, the Business Rule checks if the user has a role in common with the assignment group. If there is no common role, access to the incident is restricted, and the incident is not saved. An error message is also displayed to indicate the lack of access.

Regards,

Shravan

Please mark this as correct and helpful if it solved your query

Regards,
Shravan
Please mark this as helpful and correct answer, if this helps you

Gayatri0506
Tera Contributor

Even I have the same requirement, any suggestion will be appreciated.