Name

sn_hr_sp.EmployeeDataCollection

Description

No description available

Script

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

  mapTaskDataToSurvey: function(taskData) {

      var tableName = taskData.employee_form.table_Name;
      var condition = taskData.employee_form.condition;
      var numRecords;
      var message;

      if (!gs.nil(condition)) {
          var recordGr = new GlideRecord(tableName);
          recordGr.addEncodedQuery(condition);
          recordGr.query();
          numRecords = recordGr.getRowCount();
          if (numRecords != 1) {
              message = gs.getMessage("Cannot display the employee form because the associated condition returned {0} records. The condition must return one record.", [numRecords]);
              this.addError(taskData, message);
              return;
          }

          if (recordGr.next()) {
              if (!recordGr.canRead()) {
                  message = gs.getMessage("The user doesn't have read permission for the record. Please check the ACLs of the associated table.");
                  this.addError(taskData, message);
                  return;
              }
              mapFields();
          }
      }


      function mapFields() {
          var tempInt;
          var mappingGr = new GlideRecord('sn_hr_core_employee_form_field_mapping');
          mappingGr.addQuery('employee_form', taskData.employee_form.id);
          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', taskData.employee_form.survey_instance);
              qaGr.query();
              if (qaGr.next()) {
                  var typeString = recordGr.getElement(field).getED().getInternalType();
                  if (typeString == 'reference')
                      qaGr.reference_id = recordGr.getElement(field).toString();
                  else if (typeString == 'integer' || typeString == 'decimal' || typeString == 'longint' || typeString == 'float' || typeString == 'choice') {
                      tempInt = parseInt(recordGr.getElement(field).toString());
                      if (isNaN(tempInt))
                          qaGr.string_value = recordGr.getElement(field).toString();
                      else
                          qaGr.value = tempInt;
                  } else
                      qaGr.string_value = recordGr.getElement(field).toString();
                  qaGr.update();
              }
          }
      }

  },

  saveEmployeeForm: function(employeeFormGr, isChanged) {
      var surveyGr;
      // Create Form definition 
      if (gs.nil(employeeFormGr.form_definition)) {
          surveyGr = new GlideRecord('asmt_metric_type');
          surveyGr.initialize();
          surveyGr.name = employeeFormGr.name;
          surveyGr.description = employeeFormGr.description;
          surveyGr.introduction = '';
          surveyGr.end_note = '';
          surveyGr.evaluation_method = 'survey';
          surveyGr.active = true;
          surveyGr.duration.setDisplayValue('365 00:00:00');
          surveyGr.not_show_intro_note = true;
          return surveyGr.insert();
      } else {
          if (isChanged) {
              surveyGr = new GlideRecord('asmt_metric_type');
              if (surveyGr.get(employeeFormGr.form_definition)) {
                  surveyGr.name = employeeFormGr.name;
                  surveyGr.description = employeeFormGr.description;
                  surveyGr.active = employeeFormGr.active;
                  surveyGr.update();
                  return null;
              }
          }
      }
  },
  addError: function(taskTicket, eMessage) {
      var errorGr = new GlideRecord('sn_hr_core_employee_form_error');
      errorGr.initialize();
      errorGr.setValue('case', taskTicket.parent);
      errorGr.setValue('task', taskTicket.sys_id);
      errorGr.setValue('user', gs.getUserID());
      errorGr.setValue('description', eMessage);
      errorGr.insert();
  },

  saveUpdatedData: function(taskId) {
      var numRecords;
      var message;
      var inError = false;
      var outError = false;
      var employeeEnteredData = {};
      employeeEnteredData.fieldArray = [];
      var fieldData;
      //check if the survey is finished
      var current = new GlideRecord('sn_hr_core_task');
      if (current.get(taskId) && current.survey_instance.state == 'complete' && current.assigned_to) {
          var tableName = current.employee_form.table;
          var condition = current.employee_form.condition;
          employeeEnteredData.tableName = String(tableName);

          if (!gs.nil(condition)) {
              var recordGr = new GlideRecordSecure(tableName);
              recordGr.addEncodedQuery(condition);
              recordGr.query();
              numRecords = recordGr.getRowCount();
              if (numRecords != 1)
                  inError = true;


              if (recordGr.next())
                  saveData();
              else {
                  message = gs.getMessage("Cannot display the employee form because the associated condition returned no records. The condition must return one record.");
                  addError(message);
                  return;
              }

              if (inError || outError) {
                  //update task work notes
                  var learnMoreMessage = gs.getMessage('Click here to learn more.');
                  var taskMessage = gs.getMessage('The task:{0} completed with one or more errors.[code]<a href="/nav_to.do?uri=%2Fsn_hr_core_employee_form_error_list.do%3Fsysparm_query%3Dtask%253D{1}" target="_blank"> {2}</a> [/code]',[current.number,taskId ,learnMoreMessage]);

                  current.work_notes = taskMessage;
                  current.update();
              }

              if (inError) {
                  message = gs.getMessage("Cannot save the employee form because the associated condition returned {0} records. The condition must return one record.", [numRecords]);
                  addError(message);
              }
          }
      }

      function saveData() {

          var field;
          var needUpdate = false;
          var updateList = [];
          var mappingGr = new GlideRecord('sn_hr_core_employee_form_field_mapping');
          mappingGr.addQuery('employee_form', current.employee_form);
          mappingGr.query();

          while (mappingGr.next()) {
              field = mappingGr.getValue('field');

              //get all non read only mapped mappings
              if (mappingGr.question.read_only)
                  continue;

              fieldData = {};
              var qaGr = new GlideRecord('asmt_assessment_instance_question');
              qaGr.addQuery('metric', mappingGr.question);
              qaGr.addQuery('instance', current.survey_instance);
              qaGr.setLimit(1);
              qaGr.query();

              if (qaGr.next())
                  needUpdate = setGlideRecordValue(recordGr, field, qaGr, updateList) || needUpdate;
              else {
                  message = gs.getMessage("One or more questions were not found on the employee form. Were the questions deleted from the associated survey?");
                  addError(message);
              }

          } //while mappingGr.next()

          if (needUpdate) {
              if (!recordGr.update()) {
                  message = gs.getMessage("Glide record update failed on this table: {0}. Verify that the user has write access to the record. Also check the ACLs of the associated table, as well as the system logs.", [recordGr.getTableName()]);
                  addError(message);
              }
          }

          //update dot walk fields
          for (var updateId in updateList) {
              if (updateList.hasOwnProperty(updateId)) {
                  if (!updateList[updateId].update()) {
                      message = gs.getMessage("Glide record update failed on this table: {0}. Verify that the user has write access to the record. Also check the ACLs of the associated table, as well as the system logs.", [recordGr.getTableName()]);
                      addError(message);
                  }
              }
          } //for updateList
      } // saveData function

      function setGlideRecordValue(recordGr, field, qaGr, updateList) {
          var needUpdate = false;
          var glideElement = recordGr.getElement(field);
          var typeString = glideElement.getED().getInternalType();
          var datatype = qaGr.metric.datatype.toString();
  		

          if (typeString == 'reference') {
              if (qaGr.reference_id)
                  needUpdate = setValue(recordGr, field, glideElement, qaGr.reference_id, updateList);
          } else {
              // for 'string', 'date', and 'datetime' metric types, the value is stored as 0
              if (['string','date','datetime'].indexOf(datatype) === -1 && !gs.nil(qaGr.value))
                  needUpdate = setValue(recordGr, field, glideElement, qaGr.value, updateList);
              else if (qaGr.string_value)
                  needUpdate = setValue(recordGr, field, glideElement, qaGr.string_value, updateList, datatype);
          }
          return needUpdate;
      }

      function _isSameAsPreviousValue(oldValue, newValue, dataType, glideElement) {
          if (dataType === 'date')
              return glideElement.getDisplayValue() == newValue;
          return oldValue == newValue;
      }

      function setValue(recordGr, field, glideElement, value, updateList, dataType) {
          var refFields = field.split('.');
          var isDotWalkField = refFields.length > 1;
          var needUpdate = false;
          var recordValue = String(glideElement).trim();
          var tempValue = String(value).trim();

          fieldData.field = field;
          fieldData.oldValue = recordValue;
          fieldData.employeeNewValue = tempValue;
          employeeEnteredData.fieldArray.push(fieldData);

          if (!_isSameAsPreviousValue(recordValue, tempValue, dataType, glideElement) && !glideElement.canWrite()) {
              message = gs.getMessage("Failed to update the field:{0} on the table:{1}. Verify that the user has write access to the column. Also check the ACLs of the associated table, as well as the system logs.", [field, recordGr.getTableName()]);
              addError(message);
              return false;
          }
          if (!inError && !_isSameAsPreviousValue(recordValue, tempValue, dataType, glideElement)) {
              if (isDotWalkField)
                  updateReferenceRecord(recordGr, refFields, value, updateList);
              else {
                  if (dataType === 'date')
                      recordGr[field].setDisplayValue(value); // for date type accepted format by setValue is yyyy-MM-dd and we get the value in the current user dispay format so we need to set display value.
                  else
                      recordGr.setValue(field, value);
                  needUpdate = true;
              }
          }

          return needUpdate;
      }

      function updateReferenceRecord(parentGr, refFields, value, updateList) {
          var message = '';

          if (refFields.length < 2)
              return;

          var ge = parentGr.getElement(refFields[0]);
          var referenceGr = ge.getRefRecord();
          if (referenceGr.isNewRecord() || !referenceGr.isValidRecord()) {
              message = gs.getMessage("The reference field, {0}, is empty or invalid on this table: {1}. Make sure that the record reference is set.", [refFields[0], parentGr.getTableName()]);
              addError(message);
              return;
          }

          if (refFields.length == 2) {
              referenceGr.setValue(refFields[1], value);
              updateList[referenceGr.getUniqueValue()] = referenceGr;
          } else {
              refFields.splice(0, 1);
              updateReferenceRecord(referenceGr, refFields, value, updateList);
          }
      }

      function addError(eMessage) {
          outError = true;
          var errorGr = new GlideRecord('sn_hr_core_employee_form_error');
          errorGr.initialize();
          errorGr.setValue('case', current.parent);
          errorGr.setValue('task', current.getUniqueValue());
          errorGr.setValue('user', current.getValue('assigned_to'));
          errorGr.setValue('description', eMessage);

          //log field info
          if (employeeEnteredData.fieldArray.length > 0) {
              var payload = JSON.stringify(employeeEnteredData);
              errorGr.setValue('payload', payload);
          }
          errorGr.insert();
      }

  },
  type: 'EmployeeDataCollection'
};

Sys ID

a5fca33e872323005346caf736cb0b82

Offical Documentation

Official Docs: