Name

sn_hr_core.hr_coe_config

Description

No description available

Script

var hr_coe_config = Class.create();
hr_coe_config.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
  
  CONST: function () {
  	return {
  		HR_ADMIN: 'sn_hr_core.admin',
  		// Must ensure the order of cat/detail/service when activating/deactivating to avoid null error in business rules
  		COE_TABLE_AND_FIELD: {		
  			sn_hr_core_topic_category: 'coe', // Topic Category
  			sn_hr_core_topic_detail: 'topic_category.coe', // Topic Detail	
  			sn_hr_core_service: 'service_table',  // HR Service				
  			sc_cat_item_producer: 'table_name', // Record producer
  			sys_app_module: 'name', // Module
  		},
  		SCOPE: {
  			CURRENT: gs.getCurrentApplicationId(),
  			HR_CORE: { // Human Resources: Core
  				ID: "d4ac3fff5b311200a4656ede91f91af2",
  				NAME: "Human Resources: Core",
  			}, 
  			HR_INT: "aaa655c19f0322003be01050a57fcf65", // Human Resources: Integrations
  			HR_LE: "532da270dad21200a4656ede91f9331e",  // Human Resources: Lifecycle Events
  			HR_LE_ENT: "ee724b063b223300d901655593efc4f3",  // Human Resources: Lifecycle Events Enterprise				
  			HR_PJ: "c4ff1e762cbe2340964f0ca310007ffe",  // Human Resources: Parental Journey		
  			HR_SP: "3d1da2705b021200a4656ede91f91ab6",  // Human Resources: Service Portal
  			// if new scope is needed, that scope id would need to be added here and a new script include for RCA bypass				
  		},
  		USER_ROLE: gs.getUser().getRoles(),
  	};
  },
  
  getAllCoes: function() {
  	var retCOEJSON = {};
  	retCOEJSON.canEdit = "";
  	retCOEJSON.currentScopeName = "";
  	retCOEJSON.callerScopeName = "";
  	var currentCallerScope =  this._getScopeNameByAppId(this.CONST().SCOPE.CURRENT);
  	var activeCoe = [];

  	if (!gs.hasRole(this.CONST().HR_ADMIN))
  		return new global.JSON().encode(activeCoe);	
  	
  	retCOEJSON.activeCoe = this._getAllCoeTable();
  	
  	var roles = this.CONST().USER_ROLE;
  	var isHrAdmin = roles.indexOf('admin') > -1;
  
  	if(this.CONST().SCOPE.CURRENT != this.CONST().SCOPE.HR_CORE.ID && isHrAdmin){
  		retCOEJSON.canEdit = false;
  		retCOEJSON.currentScopeName = this.CONST().SCOPE.HR_CORE.NAME;
  		retCOEJSON.callerScopeName = currentCallerScope;
  	} else 
  		retCOEJSON.canEdit = true;

  	return new global.JSON().encode(retCOEJSON);
  },
  
  updateCoe: function () {
  	// check for hr admin role
  	if (gs.hasRole(this.CONST().HR_ADMIN)) {
  		// need for to use in map
  		var that = this;
  		var CONST = that.CONST();
  		
  		// comma seperated string from hr_license UI page
  		var deactivateList = this.getParameter('sysparm_deactive_coe');
  		var activateList = this.getParameter('sysparm_active_coe');
  		var changeListMsg = this.getParameter('sysparm_coe_change_list');			
  		
  		// loop through list of tables and deactivate all record
  		Object.keys(CONST.COE_TABLE_AND_FIELD).map(function (k) { // key
  				that._updateCoeRecords(k, CONST.COE_TABLE_AND_FIELD[k], deactivateList, changeListMsg, true);
  		});
  		
  		// HR Service activation requires that topic detail be active, so need to reverse the order
  		Object.keys(CONST.COE_TABLE_AND_FIELD).map(function (k) { // key
  			that._updateCoeRecords(k, CONST.COE_TABLE_AND_FIELD[k], activateList, changeListMsg);  // update all records
  		});
  	}
  },
  	
  /* @param tableName (STRING)
   * @param columnName (STRING)
   * @param param (STRING) - comma seperated list of table names (sn_hr_core_case_*, sn_hr_le_case)
   * @param deactivate (BOOL, optional)
   * @return STRING verification/error message on PUT
   */
  _updateCoeRecords: function (tableName, columnName, param, changeListMsg, deactivate) {
  	var tableGr = new GlideRecord(tableName);
  	
  	if (deactivate)
  		tableGr.addActiveQuery();
  	else
  		tableGr.addQuery('active', false);

  	// if updating modules, need to filter by active cases and only in HR Application App
  	if (tableName === "sys_app_module") {
  		// filter only needs to happen when deactivating Modules
  		if (deactivate)
  			param = this._filterCoeTablesByActiveCases(param);
  		tableGr.addQuery('application', '7f0370019f22120047a2d126c42e7075'); // HR Case Management		
  	}
  	
  	tableGr.addQuery(columnName, 'IN', param);
  	tableGr.query();
  	
  	while(tableGr.next()) {
  		var updated = "";
  		
  		// some records are created outside of hr_core, need to call the script include in the corresponding scope
  		// any new records outside of hr_core will need its own script include
  		switch (tableGr.getValue('sys_scope')) {
  			case this.CONST().SCOPE.HR_INT: // HR: Integration scope
  				updated = new sn_hr_integrations.hr_int_coe_config().updateCoeRecordProducerGr(tableGr, deactivate);
  				break;
  			case this.CONST().SCOPE.HR_LE_ENT: // HR: LE Enterprise scope
  				updated = new sn_hr_le_ent.hr_le_ent_coe_config().updateLeCoeGr(tableGr, deactivate);		
  				break;
  			case this.CONST().SCOPE.HR_LE: // HR: LE scope
  				updated = new sn_hr_le.hr_le_coe_config().updateLeCoeGr(tableGr, deactivate);
  				break;
  			case this.CONST().SCOPE.HR_PJ: // HR: Parental Journey scope
  				updated = new sn_hr_pj.hr_pj_coe_config().updateCoeRecordProducerGr(tableGr, deactivate);
  				break;	
  			case this.CONST().SCOPE.HR_SP: // HR: Service Portal scope
  				updated = new sn_hr_sp.hr_sp_coe_config().updateCoeRecordProducerGr(tableGr, deactivate);
  				break;
  			default:
  				tableGr.active = deactivate ? '0' : '1';
  				updated = tableGr.update();
  				break;
  		}
  		
  		if (updated && changeListMsg)
  			gs.addInfoMessage(changeListMsg);
  		
  		// if Record Producer, need to check and turn on/off category from sc_category
  		if (tableName === "sc_cat_item_producer") 
  			this._updateCoeRecords('sc_category', 'sys_id', tableGr.getValue('category'), null, deactivate);		
  	}
  },
  
  /* @param tableName (STRING)
   * @param columnName (STRING)
   * @param param (STRING) - comma seperated list of table names (sn_hr_core_case_*, sn_hr_le_case)
   * @param deactivate (BOOL, optional)
   * @return STRING verification/error message on PUT
   */	
  _getScopeNameByAppId: function (appId){
  	var scopeName = new GlideRecord('sys_scope');
  	scopeName.addQuery('sys_id',appId);
  	scopeName.query();
  	if(scopeName.next())
  		return scopeName.name.toString();
  	return "";
  },
  
  /* @param param (STRING) - comma seperated list of table names (sn_hr_core_case_*, sn_hr_le_case)
   * @return STRING filtered comma seperated list of table names
   */	
  _filterCoeTablesByActiveCases: function (param) {
  	// keep a copy of tables to filter out
  	var filteredParam = param;
  	
  	// get all HR Cases in draft, ready, awaiting approval, work in progress, awaiting acceptance state with HR Services
  	var caseGr = new GlideAggregate('sn_hr_core_case');
  	caseGr.addQuery('active', true);
  	caseGr.addQuery('hr_service.service_table', 'IN', param);
  	caseGr.groupBy('hr_service.service_table');
  	caseGr.query();
  	
  	// remove case HR Service that matches passed in table. empty commas (,,) does not affect 'IN' query
  	while (caseGr.next())
  		filteredParam = filteredParam.replace(caseGr.getValue("hr_service.service_table"), "");

  	return filteredParam;
  },
  
  _getAllCoeTable: function () {
  	var activeCoe = [];
  	var dbGr = new GlideRecord('sys_db_object');
  	dbGr.addActiveQuery();
  	dbGr.addQuery('name', 'IN', new GlideTableHierarchy("sn_hr_core_case").getTableExtensions());
  	dbGr.query();

  	while(dbGr.next()) {			
  		var coeName = dbGr.getValue('name');
  		var coeStatus = this._getCoeStatus(coeName);
  		var coeGr = new GlideRecord(coeName);
  		activeCoe.push({
  			enabled: true,
  			empty: coeStatus.empty,					
  			name: coeGr.getLabel(),
  			selected: coeStatus.active,			
  			value: dbGr.getValue('name'),
  		});
  	}
  	
  	return activeCoe;
  },
  
  /* @param coe (STRING)
   * @param active (BOOL)
   * Update the sys property sn_hr_core.inactive_tables with active/inactive COEs
   */		
  _updateHrCoreInactiveTableSysProp: function (coe, active) {		
  	var inactiveTables = gs.getProperty("sn_hr_core.inactive_tables", "");
  	// need to prevent an empty string object to be produced by split
  	var inactiveTablesArr = inactiveTables ? inactiveTables.split(",") : [];
  	var tableIndex = inactiveTablesArr.indexOf(coe);

  	// remove coe if active and in table
  	if (active && tableIndex > -1)
  		inactiveTablesArr.splice(tableIndex, 1);
  	
  	// add coe if inactive and not in table		
  	else if (!active && tableIndex === -1)
  		inactiveTablesArr.push(coe);

  	// update on above conditions
  	if (active && tableIndex > -1 || !active && tableIndex === -1) {
  		var inactiveTableGr = new GlideRecord('sys_properties');
  		inactiveTableGr.get("bc364ac90b232200ecbe6f3ef6673a39"); // sn_hr_core.inactive_tables sys_property
  		
  		if (inactiveTableGr.isValid()) {
  			inactiveTableGr.value = inactiveTablesArr.join(',');
  			inactiveTableGr.update();
  		}
  	}
  },
  
  _getCoeStatus: function (coe) {
  	var CONST = this.CONST(); // to be used later
  	var statusObj = {
  		active: false,
  		empty: true
  	};
  	
  	/* loop through list of COE tables and check for inactive record
  	 * if any inactive record, break and return false
  	 */		
  	Object.keys(CONST.COE_TABLE_AND_FIELD).map(function (k) { // key			
  		var topicCategoryGr = new GlideRecord(k);
  		
  		// skip Modules, can be active even if "off" in COE Configuration			
  		if (!topicCategoryGr.isValid() || k === "sys_app_module")
  			return statusObj;
  		
  		topicCategoryGr.addQuery(CONST.COE_TABLE_AND_FIELD[k], coe);
  		topicCategoryGr.query();
  		
  		while (topicCategoryGr.next()) {
  			statusObj.empty = false;
  			
  			if (!statusObj.active)
  				statusObj.active = !!+topicCategoryGr.getValue('active');
  		}			
  	});
  			
  	// update sn_hr_core.inactive_tables sys prop
  	this._updateHrCoreInactiveTableSysProp(coe, statusObj.active);
  	
  	return statusObj;
  },
  
  type: 'hr_coe_config'
});

Sys ID

353cc5120b6222004f526f3ef6673a48

Offical Documentation

Official Docs: