Name

sn_hr_mobile.HRTaskFieldMappingUtilsSNC

Description

Utility methods to extract values from employee form field mappings in Employee form

Script

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

  /**
   * Returns default values to be prefilled in the survey's questions as default answers
   * @param {String} taskId - The sys_id of HR task
   * @return {Object} The object containing sys_id of survey instance questions and the value to be prefilled as default answer to the corresponding question
   */
  getDefaultValuesFromTask: function(taskId) {
      var taskGr = new GlideRecord(sn_hr_core.hr.TABLE_TASK);
      var tableName;
      var condition;
      var recordGr;

      taskGr.get(taskId);

      var defaultValues = {};
      try {
          if (taskGr.hr_task_type.toString() === 'collect_Information') {
              tableName = taskGr.getElement('employee_form.table') + '';
              condition = taskGr.getElement('employee_form.condition') + '';
              if (gs.nil(condition) || !isRecordCountValid(tableName, condition))
                  return defaultValues;
              recordGr = new GlideRecord(tableName);
              recordGr.addEncodedQuery(condition);
              recordGr.query();
              recordGr.next();
              var mappingGr = new GlideRecord('sn_hr_core_employee_form_field_mapping');
              mappingGr.addQuery('employee_form', taskGr.getValue('employee_form'));
              mappingGr.query();
              while (mappingGr.next()) {
                  var field = mappingGr.getValue('field');
                  var qaGr = new GlideRecord('asmt_assessment_instance_question');
                  qaGr.addQuery('metric', mappingGr.question);
                  qaGr.addQuery('instance', taskGr.getValue('survey_instance'));
                  qaGr.query();
                  if (qaGr.next())
                      defaultValues[qaGr.getValue('sys_id')] = recordGr.getElement(field).toString();
              }
          }
      } catch (err) {
          gs.warn('HRTaskFieldMappingUtilsSNC: Error in fetching default values: ' + err);
          defaultValues = {};
      }


      return defaultValues;

      /**
       * Check if Record count is strictly 1 for given tablename and condition
       * @param {String} tableName - The table name of the mapped table 
       * @param {String} condition - The condition for the mapped table
       * @return {boolean} true if record count for table and condition is 1 else false
       */
      function isRecordCountValid(tableName, condition) {
          var recordGa = new GlideAggregate(tableName);
          recordGa.addAggregate('COUNT');
          recordGa.addEncodedQuery(condition);
          recordGa.query();
          var count = 0;
          if (recordGa.next())
              count = recordGa.getAggregate('COUNT');
          return count == 1 ? true : false;
      }
  },

  type: 'HRTaskFieldMappingUtilsSNC'
};

Sys ID

87f2b062eb03201005017994a2522874

Offical Documentation

Official Docs: