Name

sn_hr_core.hr_Criteria_Private

Description

Private util for evaluating HR criteria and underlying conditions

Script

var hr_Criteria_Private = Class.create();
hr_Criteria_Private.prototype = {
  type: 'hr_Criteria_Private',
  initialize: function() {
  	this.userTables = new GlideTableHierarchy("sys_user").getAllExtensions();
  },
  
  /* Add @param userId to @param conditionGe's condition
   *
   * @param grCondition GlideRecord [sn_hr_core_condition] Condition record
   * @param userId String sys_id of a user record
   * @return String Encoded query with @param userId added in to limit condition to a specific user
   */
  _addUserToCondition : function(grCondition, userId) {
  	var conditionString = this._replaceTargetUserID(grCondition.condition.toString(), userId);
  	var userColumn = grCondition.user_column.toString();

  	// If user_column not provided and table is an instance of sys_user, then use 'sys_id' for the user_column
  	if (gs.nil(userColumn) && !gs.nil(grCondition.table) && this.userTables.indexOf(grCondition.table.toString()) > -1)
  		userColumn = 'sys_id';

  	if (!gs.nil(userColumn)) {
  		var conditions = conditionString.split("^NQ");
  		for (var i = 0; i < conditions.length; i++)
  			conditions[i] = userColumn + '=' + userId + "^" + conditions[i];
  		conditionString = conditions.join("^NQ");
  	}

  	return conditionString;
  },
  
  /*
   * Adding the survey instance to the hr condition
   * In case of LE check for the survey instance taken as part of the existing le case
   * If no survey instance found within current le case, add the latest survey instance ever taken
   * If the survey was never taken, return a condition that always evaluates to false
   */
  _addSurveyInstance : function(grCondition, userId, caseId) {
  	var cond = '';
  	// add the survey instance taken as part of the same case (LE)
  	// In this scenario, we are only looking for any HR task associated with the LE case to have 
  	// a completed survey.
  	if (caseId) {
  		cond = this._addSurveyFromLe(grCondition, userId, caseId);
  		if (cond)
  			return cond;
  	} 
  	
  	// if no survey instance could be found within the case, find the latest one taken by the subject
  	// person on the LE case.
  	var survey = grCondition.employee_form.form_definition;
  	var grMetricResult = new GlideRecord('asmt_metric_result');
  	grMetricResult.addQuery('source_id',survey);
  	grMetricResult.addQuery('user',userId);
  	grMetricResult.orderByDesc('sys_created_on');
  	grMetricResult.query();
  	cond = '';
  	if (grMetricResult.next()) {
  		cond = 'source_id='+survey;
  		cond+= '^instance='+grMetricResult.instance+'^';
  		cond+= grCondition.condition;
  		return cond;
  	}
  	else // user has not taken the survey yet
  		return 'sys_id=-1';
  },
  
  //Add survey instance taken as part of the same Le case
  _addSurveyFromLe : function(grCondition, userId, caseId) {
  	var survey = grCondition.employee_form.form_definition;
  	var employeeForm = grCondition.employee_form;
  	var cond = '';
  	
  	var grTask = new GlideRecord('sn_hr_core_task');
  	grTask.addQuery('parent',caseId);
  	grTask.addQuery('active',false);
  	grTask.addQuery('employee_form',employeeForm);
  	grTask.query();
  	
  	if (grTask.next()) {
  		cond = 'source_id='+survey;
  		cond+= '^instance='+grTask.survey_instance+'^';
  		cond+= grCondition.condition;
  		return cond;
  	}
  	else {
  		var grCase = new GlideRecord('sn_hr_core_case');
  		grCase.addQuery('parent',caseId);
  		grCase.query();
  		while (grCase.next()) {
  			cond = this._addSurveyFromLe(grCondition, userId, grCase.getUniqueValue());
  			if (cond)
  				return cond;
  		}
  	}
  	return cond;
  },

  /* Replace instances of @var QUERY_KEYWORD in @param condition
   *
   * @param condition String Encoded query
   * @param userId String sys_id of a user record
   * @return String Condition string with @var QUERY_KEYWORD replaced by @param userId in @param condition
   */
  _replaceTargetUserID : function(condition, userId) {
  	var QUERY_KEYWORD = 'javascript:{targetUserID}';

  	if (condition.indexOf(QUERY_KEYWORD) > -1)
  		condition = condition.replace(new RegExp(QUERY_KEYWORD, "gi"), userId);

  	return condition;
  },

  /* Determine if a condition is met (i.e. a record exists for the condition)
   *
   * @param table String Name of table to query
   * @param query String Encoded query to apply to table
   * @return boolean Whether a record exists for @param table and @param query
   */
  _meetsHrCondition : function(table, query) {
  	var queryGr = new GlideRecord(table);
  	queryGr.addEncodedQuery(query);
  	queryGr.setLimit(1);
  	queryGr.query();
  	return queryGr.hasNext();
  }

};

Sys ID

69ade744730113006e9be9c54cf6a75e

Offical Documentation

Official Docs: