Name

sn_hr_core.hr_FulfillmentInstructions

Description

No description available

Script

var hr_FulfillmentInstructions = Class.create();
hr_FulfillmentInstructions.prototype = {
  initialize: function() {},

  /**
  *  Retrieves the Fulfillment Instructions to use for the HR Case with the specified sys_id 
  * 
  *  For active cases, calculates, saves, and returns the latest instructions
  *  For inactive cases, retrieves the stored instructions from the record
  *
  * @param caseId - sys_id of the HR Case to check
  *
  * @returns JSON object with the following properties:
  *    instructions - The latest instructions for this case
  *    refreshed - true if the instructions were updated
  */
  getLatestCaseInstructions: function (caseId, usePermaLinks) {
  	var gr = new GlideRecord(hr.TABLE_CASE);
  	var result = {refreshed : false};
  	var removeNewlines = function(text) { return text.replace(/(\r\n|\n|\r)/gm,''); };
  	
  	if (caseId && gr.get(caseId)) {
  		var existingInstructions = removeNewlines(gr.getValue('fulfillment_instructions') || '');
  		var kbNumbers = this._getArticleNumbers(existingInstructions);
  		
  		if (gr.active) {
  			//Instructions without KB links
  			var fiOld = this._getFI(existingInstructions);

  			// Use record class for instruction lookup to include instructions with COE-specific field conditions
  			var grCaseExtension = gr;
  			var recordClassName = gr.getRecordClassName();
  			if (recordClassName != hr.TABLE_CASE) {
  				grCaseExtension = new GlideRecord(recordClassName);
  				grCaseExtension.get(caseId);
  			}
  			
  			//New Instructions and Kb links
  			result.instructions = removeNewlines(this.getInstructions(grCaseExtension));
  			//New Fulfillment instructions without KB Links
  			var fiNew = this._getFI(result.instructions);


  			//Arrays containg knowledge article links
  			var oldKBsArray = kbNumbers;
  			kbNumbers = this._getArticleNumbers(result.instructions);

  			var instructionsChanged = fiOld != fiNew;	
  			var kbChanged = oldKBsArray.toString() != kbNumbers.toString();

  			if (instructionsChanged || kbChanged) {	
  				gr.fulfillment_instructions = result.instructions;
  				gr.setWorkflow(false);
  				result.refreshed = gr.update() != null;
  			}
  		}
  		else 
  			result.instructions = existingInstructions;
  		
  		if (usePermaLinks == 'false')
  			result.instructions = this._replaceArticlePermaLinks(result.instructions, kbNumbers);
  	}
  	
  	return result;
  },
  
  /**
  * Calculates the Fulfillment Instructions for this record, given the definitions in the 'sn_hr_core_fulfillment_instruction' table
  *
  * gr - GlideRecord which the instructions should apply for
  * returns string representing the ordered combination of instructions relevant to the gr input
  */
  getInstructions : function(gr) {
  	if (!gr || typeof gr.getRecordClassName != 'function')
  		return;
  	
  	// Find instructions relevant to this record's table
  	var grInstructionList = this._getOrderedInstructionRecordsForRecord(gr);
  	var instructions = '';
  	
  	// If each instruction's condition matches to the record, append it in the correct order
  	while (grInstructionList.next()) {
  		if (ScopedGlideFilter.checkRecord(gr, grInstructionList.condition)){
  			if (!grInstructionList.instructions.nil())
  				instructions += grInstructionList.getDisplayValue('instructions');
  			if (grInstructionList.kb_articles){
  				var kbArticleIdsArray = grInstructionList.getValue('kb_articles').split(',');
  				var articleLinks = this._getKBArticles(kbArticleIdsArray);
  				if(articleLinks)
  					instructions += articleLinks;
  			}
  		}
  	}
  	
  	return instructions;
  },
  
  _getKBArticles : function(kbArticleIdsArray) {
  	var articleLinks = '';
  	
  	kbArticleIdsArray.forEach(function (kbId){
  		
  		var kb = new GlideRecord('kb_knowledge');
  		if(kb.get(kbId)){
  			
  			var title = kb.getValue('number') + ' - ' + kb.getValue('short_description');
  			articleLinks += '<a id="sn_hr_fi_kbs_'+kb.getValue('number')+'" target="_blank" href="kb_view.do?sysparm_article&#61;' + kb.getValue('number') + '" rel="noopener noreferrer nofollow">'+title+'</a><br />';
  		}
  	});
  	
  	if (articleLinks)
  		return articleLinks+'<br />';
  },
  	
  _getFI : function (text) {
  	var articleLinksArray = this._getArticleLinks(text);
  	if(articleLinksArray.length) {
  		articleLinksArray.forEach(function(link) {
  			text = text.replace(link,'');
  		});
  	}
  	return text;
  },
  
  _getArticleLinks : function (text) {
  	var articleLinks = [];
  	var kbStartIndex = function(text) { return text.indexOf('<a id="sn_hr_fi_kbs_');};
  	var kbEndIndex = function(text) { return text.indexOf('</a><br />',kbStartIndex(text));};
  	
  	while (kbStartIndex(text) > -1) {
  		articleLinks.push(text.substring(kbStartIndex(text),kbEndIndex(text)+10));
  		text = text.replace(text.substring(kbStartIndex(text),kbEndIndex(text)+10),'');
  	}
  	return articleLinks;
  },
  
  _getArticleNumbers : function (text) {
  	var articleLinks = this._getArticleLinks(text);
  	var kbNumbers = [];
  	var LENGTH_OFARTICLE_NUMBER = 9;

  	articleLinks.forEach(function (link) {
  		var articleStartIndex = link.indexOf('sysparm_article&#61;') + 20;
  		if(articleStartIndex) {
  			var articleEndIndex = articleStartIndex + LENGTH_OFARTICLE_NUMBER; 
  			var articleNumber = link.substring(articleStartIndex,articleEndIndex);
  			if(articleNumber)
  				kbNumbers.push(articleNumber);
  		}
  	});
  	return kbNumbers;
  },

  
  _getOrderedInstructionRecordsForRecord : function(gr) {
  	var grInstructions = new GlideRecord('sn_hr_core_fulfillment_instructions');
  	grInstructions.addActiveQuery();
  	grInstructions.addQuery('table', 'IN', new GlideTableHierarchy(gr.getRecordClassName()).getTables());
  	grInstructions.orderBy('order');
  	grInstructions.orderBy('sys_created_on');
  	grInstructions.query();
  	
  	return grInstructions;
  },
  
  _replaceArticlePermaLinks : function(existingInstructions, articleNumbersArr) {
  	var latestArticles = new global.HRSecurityUtils().getLatestKnowledgeRecordIds(articleNumbersArr);
  	var updatedInstructions = existingInstructions;
  			
  	for (var articleNumber in latestArticles) {
  		var tokenToReplace = 'sysparm_article&#61;' + articleNumber;
  		var replaceWith = 'sys_kb_id&#61;' + latestArticles[articleNumber];
  		
  		while (updatedInstructions.indexOf(tokenToReplace) != -1)
  			updatedInstructions = updatedInstructions.replace(tokenToReplace, replaceWith);
  	}
  	
  	return updatedInstructions;
  },
  
  type: 'hr_FulfillmentInstructions'
};

Sys ID

deadb4ef3b0723003585802b13efc4ad

Offical Documentation

Official Docs: