Name

sn_hr_core.HRClientRoleRuleUtilsSNC

Description

No description available

Script

var HRClientRoleRuleUtilsSNC = Class.create();
HRClientRoleRuleUtilsSNC.prototype = {
  initialize: function() {
      this.PROCESS_CLIENT_ROLE_RULE_CHANGE_FLOW_ACTION = 'ddfa0c8673e710102983fd2927f6a763';
  },

  isRuleChangeInProgress: function() {
      var flowContextGr = new GlideRecord('sys_flow_context');
      flowContextGr.addQuery("flow", this.PROCESS_CLIENT_ROLE_RULE_CHANGE_FLOW_ACTION);
      flowContextGr.addQuery("state", "IN", 'QUEUED,IN_PROGRESS,WAITING,CONTINUE_SYNC');
      flowContextGr.setLimit(1);
      flowContextGr.query();
      return flowContextGr.hasNext();
  },

  invokeProcessRuleChangeAction: function(current, previous) {
      var params = {
          active: {
              previous: previous.active.toString(),
              current: current.active.toString()
          },
          condition: {
              previous: previous.condition.condition.toString(),
              current: current.condition.condition.toString()
          },
          roleId: {
              previous: previous.role.toString(),
              current: current.role.toString()
          }
      };

      try {
          var inputs = {};
          inputs['client_role_rule'] = current;
          inputs['operation'] = current.operation();
          inputs['change_params'] = JSON.stringify(params);
          
          sn_fd.FlowAPI.getRunner().subflow('sn_hr_core.process_client_role_rule_change').inBackground().withInputs(inputs).run();
          gs.addInfoMessage(gs.getMessage('Rule change is submitted for processing'));

      } catch (ex) {
          var message = ex.getMessage();
          gs.error(message);
      }
  },

  processRuleChange: function(clientRoleRuleGr, operation, params) {
      var hrUtils = new hr_Utils();

      if (operation == 'insert') {
          hrUtils.assignRolesPerCondition(params.condition.current, params.roleId.current);
          return;
      }

      if (operation == 'update') {
          var activeChanged = params.active.current != params.active.previous;

          if (activeChanged && params.active.current == 'false') {
              hrUtils.removePreviouslyAssignedRoles(params.condition.current, this._getRoleName(params.roleId.current));
              return;
          }

          if (activeChanged && params.active.current == 'true') {
              hrUtils.assignRolesPerCondition(params.condition.current, params.roleId.current);
              return;
          }

          hrUtils.removePreviouslyAssignedRoles(params.condition.previous, this._getRoleName(params.roleId.previous));
          hrUtils.assignRolesPerCondition(params.condition.current, params.roleId.current);
      }

      if (operation == 'delete')
          hrUtils.removePreviouslyAssignedRoles(params.condition.previous, this._getRoleName(params.roleId.previous));
  },

  _getRoleName: function(roleId) {
      var roleName = new GlideRecord('sys_user_role');
      if (roleName.get(roleId))
          return roleName.name;

      return '';
  },

  type: 'HRClientRoleRuleUtilsSNC'
};

Sys ID

eecfd8ce730b10102983fd2927f6a78e

Offical Documentation

Official Docs: