Name

sn_hr_core.ReclassifyCaseTransfer

Description

Implements extension point sn_hr_core.HRCaseTransfer Specifies HR Case transfer methods and whether they apply for a specific record.

Script

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

  /*
   * Returns the sys_id of configuration record used for enablement and visibility
   *	
   * @returns {string} The sys_id of the the corresponding 'sn_hr_core_transfer_case_config' record
   */
  getConfigurationId: function() {
      return '8885c7bf3b0033003585802b13efc4fc';
  },

  /*
   * Determines whether this transfer method should be selectable for a specific HR Case record.
   * Keep default value (return true) will not add any case-specific filtering.
   * 
   * @param {GlideRecord} grCase - the record of the case to transfer from
   *
   * @returns {boolean} Whether or not this transfer method should apply for a specific HR Case record. 
   */
  isCaseEligibleForMethod: function(grCase) {
      return true;
  },

  /*
   * Performs the HR Case transfer using this transfer method and provided input data
   *
   * @param {GlideRecord} grCase - the record of the case to transfer from
   * @param {JSON Object} input - Contains properties for specified details required to complete the transfer (e.g. the sys_id of the destination HR Service)
   *
   * @returns newRecord {GlideRecord} for the newly created record. 
   */
  transferCase: function(grCase, inputs) {
      var selectedService = inputs["selected_service"];
      var transferUtils = new hr_TransferCase();
      var leTransferUtils = GlidePluginManager.isActive("com.sn_hr_lifecycle_events") ? new sn_hr_le.hr_LETransferUtils() : null;
      var erTransferUtils = GlidePluginManager.isActive("com.sn_hr_employee_relations") ? new sn_hr_er.er_TransferUtils() : null;
      var service = new GlideRecord("sn_hr_core_service");

      if (!service.get(selectedService)) {
          gs.addErrorMessage(gs.getMessage("Could not find selected service"));
          return;
      }

      // Create new case
      var newRecord = transferUtils._createCaseFromService(service);
      var originalRecordSysId = grCase.getUniqueValue();

      // Copy fields from original case
      transferUtils._copyFields(grCase, newRecord);
      newRecord.hr_service = selectedService;
      newRecord.transferred_from = originalRecordSysId;

      if (service.template.toString())
          new sn_hr_core.hr_TemplateUtils().applyBefore(service.template.toString(), newRecord, false);
  	
      var oldNumber = grCase.getValue("number");
      transferUtils.swapNumber(grCase, newRecord);
      if (leTransferUtils && leTransferUtils.isLECase(grCase))
          leTransferUtils.setWorkflow(grCase, false);
      else if (erTransferUtils && erTransferUtils.isERCase(grCase))
          erTransferUtils.setWorkflow(grCase, false);
      else
          grCase.setWorkflow(false);

      grCase.update();
      var newRecordSysId = newRecord.insert();
      if (!newRecordSysId) {
          if (leTransferUtils && leTransferUtils.isLECase(grCase))
              leTransferUtils.setValue(grCase, "number", oldNumber);
          else if (erTransferUtils && erTransferUtils.isERCase(grCase))
              erTransferUtils.setValue(grCase, "number", oldNumber);
          else
              grCase.setValue("number", oldNumber);
          grCase.update();
          gs.addErrorMessage(gs.getMessage("Failed to insert a new case"));
          return;
      }
  	
      var newRecordClass = newRecord.getRecordClassName();
      newRecord = new GlideRecord(newRecordClass);
      if (!newRecord.get(newRecordSysId)) {
  		// If the newRecord cannot be queried, try to use setWorkflow(false)
  		try {
  			// Declare a new GlideRecord as isValid returns false if a previous '.get' fails
  			newRecord = new GlideRecord(newRecordClass);
  			
  			// If table is in non-sn_hr_core scope, then query as sn_hr_core_case due to security restrictions on setWorkflow API
  			if ((leTransferUtils && leTransferUtils.isLECase(newRecord))
  					|| (erTransferUtils && erTransferUtils.isERCase(newRecord)))
  				newRecord = new GlideRecord('sn_hr_core_case');
  					
  			newRecord.setWorkflow(false);
  			newRecord.get(newRecordSysId);
  			newRecord.setWorkflow(true);
  			
  			if (!newRecord.isValidRecord())
  				throw this.type + ': The new case could not be found - ' + newRecordSysId;
  		} catch(e) {
  			// Let the transfer continue but give an error message
  			gs.error('Transfer case exception: ' + e);
  			gs.addErrorMessage(gs.getMessage('The new case could not be found or you do not have access to view it'));
  		}
  	}
  
      // Copy work notes and comments to the new record. Not necessary when a new case number is assigned because
      // old case is still accessible and any new comments will be copied forward into the new case.
      if (!newRecord.getValue("universal_request")) {
          transferUtils._copyAttachments(grCase, newRecord);
          transferUtils._copyWorkNotes(grCase, newRecord);
      } else {
          transferUtils._copyOnlyWorkNotes(grCase, newRecord);
          transferUtils._copyCommentsForUR(grCase, newRecord);
      }
  	
      // Cancel original case
      // Check for appropriate scopes in order to change workflows correctly
      if (leTransferUtils && leTransferUtils.isLECase(grCase))
          leTransferUtils.setWorkflow(grCase, true);
      else if (erTransferUtils && erTransferUtils.isERCase(grCase))
          erTransferUtils.setWorkflow(grCase, true);
      else
          grCase.setWorkflow(true);
      grCase.state = hr_Constants.CASE_CANCELLED;
      grCase.transferred_to = newRecordSysId;

      if (!grCase.update()) {
          gs.addErrorMessage(gs.getMessage("Failed to close original case"));
          return;
      }

      // Copy original creation date
      newRecord.sys_created_on = grCase.sys_created_on;
      if (newRecord.isValidRecord())
          newRecord.update();
      else
          gs.addErrorMessage(gs.getMessage('Some fields may not have been transferred over to the new case as you do not have access to the record'));

      // Copy interaction_related_record(s)
      transferUtils.copyInteractionRelatedRecords(grCase, newRecord);

      return newRecord;
  },

  type: 'ReclassifyCaseTransfer'
};

Sys ID

e1ed37fe3b4c33003585802b13efc414

Offical Documentation

Official Docs: