Name

sn_hr_er.er_AssignmentUtil

Description

No description available

Script

var er_AssignmentUtil = Class.create();
er_AssignmentUtil.prototype = Object.extendsObject(sn_hr_core.hr_AssignmentUtil, {

  
  getEmpRelAgentsForLockedCaseOrderLeastLoaded: function(taskRecord,ignoreAgentsList) {
  	var agentsCountrySkills = this.getEmpRelAgentsByCountryAndSkillsOrderLeastLoaded(taskRecord,ignoreAgentsList);
  	var lockChecker = new sn_hr_er.er_SecurityUtils();
  	var eligible = [];
  	for (var i = 0; i < agentsCountrySkills.length; i++) {
  		// check eligibility to view locked cases
  		if (lockChecker.canSeeLockedCase(taskRecord, agentsCountrySkills[i]))
  			eligible.push(agentsCountrySkills[i]);
  	}

  	// if no eligible agents are found, query based upon skills only and then provide same locking test
  	if (eligible.length == 0) {
  		var agentsSkill = this.getAgentsBySkillOrderLeastLoaded(taskRecord,ignoreAgentsList);
  		
  		var agentsSkillSubset;
  		if (agentsCountrySkills.length > 0) {
  			// this list is a superset of the above list, and all those were ineligible, so remove those
  			agentsSkillSubset = agentsSkill.filter( function( el ) {
  				return agentsCountrySkills.indexOf(el) < 0;
  			} );
  		} else
  			agentsSkillSubset = agentsSkill;
  		
  		for (var j = 0; j < agentsSkillSubset.length; j++) {
  			if (lockChecker.canSeeLockedCase(taskRecord, agentsSkillSubset[j]))
  				eligible.push(agentsSkillSubset[j]);
  		}
  	}

  	return eligible;
  },
  
  /**
  * Get agents for an employee relations (ER) case or an HR task whose parent is an ER case, taking into account 
  * country and skills. Since ER cases do not use subject person, the country logic is different for ER cases.
  * @param taskRecord GlideRecord representing either an ER case or an HR task whose parent is an ER case.
  * @return Array of sys ids pointing to sys_user records.
  */
  getEmpRelAgentsByCountryAndSkillsOrderLeastLoaded: function(taskRecord,ignoreAgentsList) {
  	var ids = [];
  	if (!taskRecord || taskRecord.assignment_group.nil())
  		return ids;

  	// if no skills required for task just return agents by country
  	if (taskRecord.skills.nil())
  		return this._getEmpRelAgentsByCountryOrderLeastLoaded(taskRecord,ignoreAgentsList); // special logic as ER cases have no subject person

  	// if no country, just return agents by skill
  	var empRelCountry = this._getEmpRelCountry(taskRecord);
  	if (!empRelCountry || empRelCountry.length == 0)
  		return this.getAgentsBySkillOrderLeastLoaded(taskRecord,ignoreAgentsList);

  	// we have a country and skills, so need to get both and intersect
  	var agentsWithSkills = this.getAgentsBySkill(taskRecord,ignoreAgentsList);
  	var agentsWithCountry = this._getEmpRelAgentsByCountry(empRelCountry, taskRecord,ignoreAgentsList);

  	var arrayUtil = new global.ArrayUtil();
  	var agents = arrayUtil.intersect(agentsWithSkills, agentsWithCountry);
  	// intersect call doesn't guarantee that it will preserve order, so want to get counts and sort at end, not intermediate steps
  	ids = this._sortAgentsByLeastLoaded(agents);
  	return ids;
  },

  /**
  * This is used when there were no required skills. In that case, we will just try to match on country,
  * ordered by least loaded. If there is no country, return empty array.
  * @param taskRecord GlideRecord representing either an ER case or an HR task whose parent is an ER case
  * @return Array of sys ids pointing to sys_user records
  */
  _getEmpRelAgentsByCountryOrderLeastLoaded: function(taskRecord,ignoreAgentsList) {
  	var empRelCountry = this._getEmpRelCountry(taskRecord);
  	var agents = this._getEmpRelAgentsByCountry(empRelCountry, taskRecord,ignoreAgentsList);
  	var ids = this._sortAgentsByLeastLoaded(agents);
  	return ids;
  },

  /**
  * Use the country and the assignment group to determine suitable agents.
  * @param empRelCountry String The country to use when attempting to find matching agents
  * @param taskRecord GlideRecord The case or HR task
  * @return Array of sys ids points to sys user records
  */
  _getEmpRelAgentsByCountry: function(empRelCountry, taskRecord,ignoreAgentsList) {
  	var agents = [];
  	if (!taskRecord || taskRecord.assignment_group.nil())
  		return agents;

  	// this method should only be called if we have a country
  	if (!empRelCountry)
  		return agents;

  	var grGroupMember = new GlideRecord('sys_user_grmember');
  	grGroupMember.addQuery('group', taskRecord.assignment_group);
  	grGroupMember.addNotNullQuery('user.location');
  	grGroupMember.addQuery('user.active', true);
  	if(ignoreAgentsList) 
  		grGroupMember.addQuery("user","NOT IN",ignoreAgentsList);
  	grGroupMember.query();

  	while (grGroupMember.next()) {
  		var locSysId = grGroupMember.user.location;
  		var grLocation = new GlideRecord('cmn_location');
  		if (grLocation.get(locSysId)) {
  			var agentCountry = grLocation.getValue('country');
  			if (agentCountry && agentCountry == empRelCountry)
  				agents.push(grGroupMember.getValue('user'));
  			else {
  				var parentLocation = grLocation.getValue('parent');
  				if (parentLocation) {
  					agentCountry = grLocation.parent.country;
  					if (agentCountry && agentCountry == empRelCountry)
  						agents.push(grGroupMember.getValue('user').toString());
  				}
  			}
  		}
  	}
  	return agents;
  },

  /**
  * Special logic for employee relations as subject person is not used. Input may be either an ER case or an HR task, so if it is an
  * HR task, get the parent case and use that to determine the country. First look at the opened for country. But if opened for 
  * isn't set (it is optional in ER), then look at involved persons on the case (first complainants, then subjects of allegation).
  * @param taskRecord GlideRecord representing either an ER case or an HR task whose parent is an ER case.
  * @return String representing country name, or empty string if country isn't determined
  */
  _getEmpRelCountry: function(taskRecord) {

  	if (!(taskRecord instanceof GlideRecord) ||  !taskRecord.canRead())
  		return '';
  
  	// if the input is an hr task, get its parent and make sure it is an ER case
  	var grTask;
  	if (new GlideTableHierarchy(taskRecord.getRecordClassName()).getTables().indexOf('sn_hr_core_task') > -1) {
  		// set grTask to parent
  		var parentGr = taskRecord.parent.getRefRecord();

  		if (parentGr.isValidRecord() && parentGr instanceof GlideRecord && parentGr.canRead() && (new GlideTableHierarchy(parentGr.getRecordClassName()).getTables().indexOf('sn_hr_er_case') > -1))
  			grTask = parentGr;
  		else
  			return '';
  	} else 
  		grTask = taskRecord;

  	var openedForCountry = this._getOpenedForCountry(grTask);
  	if (openedForCountry && !gs.nil('openedForCountry'))
  		return openedForCountry;

  	// no opened for country, go to backup plan
  	var country = this._getEmpRelCountryByInvolvedParty(grTask, 'complainant');
  	if (country && !gs.nil(country))
  		return country;
  	country = this._getEmpRelCountryByInvolvedParty(grTask, 'respondent');
  	if (country && !gs.nil(country))
  		return country;
  	return '';
  },

  /**
  * Attempt to get country name via the opened for user
  * @param grCase GlideRecord representing an ER case.
  * @return String representing country name, or empty string if country not determined
  */
  _getOpenedForCountry: function(grCase) {
  	
  	if (!grCase || gs.nil(grCase))
  		return '';

  	// Verify that input is a glide record and extends task
  	if (!(grCase instanceof GlideRecord) || !grCase.canRead() || new GlideTableHierarchy(grCase.getRecordClassName()).getTables().indexOf('task') == -1)
  		return '';

  	if (grCase.isValidField('opened_for') && grCase.opened_for && grCase.opened_for.location)
  		return grCase.opened_for.location.country;

  	return '';
  },

  /** 
  * If there is no opened for country, try to get via an involved party on the case.
  * @param taskRecord GlideRecord The HR Case record
  * @param ipType String the "value" of the involved party type
  * @return String representing the country name
  */
  _getEmpRelCountryByInvolvedParty: function(taskRecord, ipType) {
  	var grInvolvedParty = new GlideRecord('sn_hr_er_involved_party');
  	grInvolvedParty.addQuery('hr_case', taskRecord.getUniqueValue());
  	grInvolvedParty.addQuery('type', ipType);
  	grInvolvedParty.addQuery('not_in_system', false);
  	grInvolvedParty.addNotNullQuery('user.location.country');
  	grInvolvedParty.orderBy('user');
  	grInvolvedParty.setLimit(1);
  	grInvolvedParty.query();

  	if (grInvolvedParty.next())
  		return grInvolvedParty.getElement('user.location.country').toString();

  	return '';
  },
  
  getIgnoreAgentsList: function(record) {
  	var involved = [];
  	var involvedPartyRecord = new GlideRecord ("sn_hr_er_involved_party"); 
  	involvedPartyRecord.addQuery("hr_case",record.getUniqueValue());
  	involvedPartyRecord.query();
  	while (involvedPartyRecord.next()) 
  		involved.push((involvedPartyRecord.user).toString()); 
  	
  	if(involved.indexOf((record.opened_for).toString()) == -1) 
  		involved.push((record.opened_for).toString());
  	return involved.toString();
  	
  },
  type: 'er_AssignmentUtil'
});

Sys ID

c78b6e203b3b0010d901655593efc44b

Offical Documentation

Official Docs: