Name
sn_hr_core.hr_CaseUtils
Description
No description available
Script
var hr_CaseUtils = Class.create();
hr_CaseUtils.prototype = {
initialize : function(_case, _gs) {
if (!_case)
return;
this._case = _case;
this._gs = _gs || gs;
// Other object scripts have a reference here as they are used throughout
this.hrProfile = new hr_Profile(this._case.hr_profile, this._gs);
this.hrUtils = new hr_Utils();
this._ignoreFields = {
'opened_for' : '',
'priority' : '',
'short_description' : '',
'description' : ''
};
this._requiresUserOrProfileCreation = {
'request_onboarding':'',
'new_hire_journey':''
};
},
populateCase : function(service, questions, source) {
var hrServiceGr = new GlideRecord(hr.TABLE_SERVICE);
if (hrServiceGr.get(service) && hrServiceGr.getValue('active')) {
var serviceName = hrServiceGr.getValue('name');
var serviceValue = hrServiceGr.getValue('value');
this._logDebug("Creating new HR Case record for service name: " + serviceName);
this._setServiceFields(hrServiceGr);
this._setGeneralFields(serviceValue, questions);
this._setCommonFields(hrServiceGr, questions, source);
var extPoints = new GlideScriptedExtensionPoint().getExtensions('sn_hr_core.HRPopulateCaseFields');
for(var i = 0; i < extPoints.length; i++) {
if (extPoints[i].handles(hrServiceGr))
extPoints[i].setFields(hrServiceGr, this._case);
}
}
},
/*
* Updates the HR Service related fields into the Case.
* Note: template kicked off when case updated (after this script runs)
*
* @param hrServiceGr The service glide record
*/
_setServiceFields : function(hrServiceGr) {
this._case.hr_service = hrServiceGr.getUniqueValue();
this._case.topic_detail = hrServiceGr.topic_detail;
this._case.topic_category = hrServiceGr.topic_detail.topic_category;
this._case.template = hrServiceGr.template;
},
/*
* Updates the general HR Case related objects like the InfoMessage
*
* @param producer Holds questions/answers (Is not a record producer glide record)
* @param category The category of the case, also drives the template used in case
*/
_setGeneralFields : function(serviceValue, questions) {
var parameters = this._getParametersFromQuestions(questions);
// Create user and profile for the subject person
if (this._requiresUserOrProfileCreation.hasOwnProperty(serviceValue)){
parameters['onboarding'] = true;
var subject_person_profile = this.hrProfile.createOrGetProfileFromParameters(parameters); // create subject person user and profile
this._case.subject_person = subject_person_profile.user.sys_id;
}
// Retrieve the profile of the 'opened_for' user
// a question might have been mapped to opened_for, in which case we grab from case
if (!parameters['opened_for']) {
if (this._case.opened_for)
parameters['opened_for'] = this._case.opened_for;
else
parameters['opened_for'] = gs.getUserID();
}
//Set opened for profile
this._grProfile = this.hrProfile.getProfileFromParameters(parameters);
// Retrieve the job information from parameters
// In some cases, a question might have been mapped to subject_person_job field, in which case we grab from case
if (parameters['job'])
this._job = parameters['job'];
else
if (this._case.subject_person_job)
this._job = this._case.subject_person_job;
else if(parameters['concurrent_job'] && parameters['concurrent_job'] == 'true')
this._job = this.hrUtils.createJobFromParameters(this._grProfile, parameters, false);
},
/*
* Set the common Case fields across all categories.
* Note: this._case is the new Case
*/
_setCommonFields : function(hrServiceGr, questions, source) {
var logged_in = gs.getUserDisplayName();
var serviceName = hrServiceGr.getDisplayValue('name');
var serviceValue = hrServiceGr.getValue('value');
this._case.contact_type = source;
// a question might have been mapped to opened_for and we do not want to override
if (!this._case.opened_for)
this._case.opened_for = this._grProfile.user.sys_id;
this._case.hr_profile = this._grProfile.sys_id;
var spUser = this._case.subject_person || this._case.opened_for;
this._case.subject_person_job = this._job? this._job : this.hrUtils.getPrimaryJob(spUser);
var openedForPriority = this._getPriority(this._grProfile.user, this._case.template);
var subjectPersonPriority = this._getPriority(this._case.subject_person, this._case.template);
var highestPriority = openedForPriority < subjectPersonPriority ? openedForPriority : subjectPersonPriority;
this._case.priority = highestPriority;
this._case.payload = new global.JSON().encode(this._getParametersFromQuestions(questions));
this._case.comments = gs.getMessage('User {0} has initiated a {1} request', [ logged_in, serviceName ]);
// Include subject person's name in short description of Performance (PIP) related cases
if ([hr.INITIATE_PIP_SERVICE, hr.PIP_ASSESSMENT_SERVICE].indexOf(hrServiceGr.sys_id.toString()) != -1)
this._setShortDescription(serviceName, this._case.subject_person.name);
else
this._setShortDescription(serviceName, this._grProfile.user.name);
var description = this._getDescriptionFromAnswers(questions, serviceValue);
this._case.description = description;
this._case.rich_description = description.replace(/\n/g,"<br>");
},
_setShortDescription: function(serviceName, username) {
this._case.short_description = gs.getMessage("{0} case for {1}", [ serviceName, username ]);
},
/*
* Builds a description field with changed or new variables. Has to determine the original value and
* display value for each of the fields coming in.
*/
_getDescriptionFromAnswers : function(questions, hrServiceValue) {
var filledValues = '';
for (var i = 0; i < questions.length; i++) {
var question = questions[i];
// Ignore the 'Opened for', 'priority' fields
if (!this._ignoreFields.hasOwnProperty(question.name) && question.question.trim() != "") {
// Checking that the answer does not match the fields original value
var originalDisplayValue = this.hrProfile.getDisplayValue(this._grProfile, question.name, true);
var originalValue = this.hrProfile.getDisplayValue(this._grProfile, question.name, false);
if ((question.answer != originalValue && question.answer != originalDisplayValue) || this._requiresUserOrProfileCreation.hasOwnProperty(hrServiceValue)) {
// Produces a string giving 'Field: newValue (original value: oldValue)'
filledValues += question.question + ': ' + question.answerDisplayValue;
if ((originalValue || originalDisplayValue) && !this._requiresUserOrProfileCreation.hasOwnProperty(hrServiceValue)) {
var txt = gs.getMessage(' (original value: {0})', originalDisplayValue);
filledValues += txt;
}
filledValues += '\n\n';
}
}
}
var additionalDescription = '';
if (filledValues) {
var spacer = (gs.nil(this._case.description)) ? '': '\n\n' ;
var msg = gs.getMessage('The following fields have been provided:');
additionalDescription = spacer + msg + '\n\n' + filledValues;
}
return this._case.description + additionalDescription;
},
/*
* This function is used in Sign Document UI action
* Moved the condition here due to condition field length limit
*/
showSignUIAction : function(current) {
return current.state==18 && current.pdf_template && (gs.nil(current.pdf_template.template_type) || current.pdf_template.template_type=='hr_pdf_templates') && new global.HRSecurityUtils().hasSignatureOnForm(current);
},
getQuestion : function(question, variableName, answer, fieldRaw, variableOrder, displayValue) {
return {
question : question+"",
name : variableName+"",
answer : answer+"",
raw : fieldRaw+"",
order : variableOrder+"",
answerDisplayValue : displayValue+""
};
},
/*
* Simplify the complex question object into a simpler object
*/
_getParametersFromQuestions : function(questions) {
var parameters = {};
for (var i = 0; i < questions.length; i++) {
parameters[questions[i].name] = questions[i].raw;
}
return parameters;
},
_getPriority : function(user, templateId) {
if (user.vip == true) {
var defaultPriority = new hr_CaseAjax().getDefaultVIPPriority();
if (defaultPriority)
return defaultPriority;
else
return '2'; // high
} else {
var result = this._getTemplateProperty(templateId, 'priority');
if (!gs.nil(result))
return result;
else
return '4'; // low
}
},
_getTemplateProperty : function(templateSysId, field) {
return new sn_hr_core.hr_TemplateUtils()._getTemplateProperty(templateSysId, field);
},
/*
* Convenience method to prevent the code becoming unreadable from the useful debug statements
*/
_logDebug : function(str) {
if (gs.isDebugging())
gs.debug(str);
},
caseLink: function(caseGr) {
if (!caseGr || !caseGr.getUniqueValue())
return;
var sysId = caseGr.getUniqueValue();
var tableName = caseGr.sys_class_name;
var number = caseGr.number;
var caseUrl = '<a href="/nav_to.do?uri=/' + tableName + '.do' + '?sys_id=' + sysId + '" target="_blank">' + number + '</a>';
return caseUrl;
},
_getActivitySetContextCases: function(caseId) {
var grContext = new GlideRecord('sn_hr_le_activity_set_context');
grContext.addQuery('hr_case', caseId);
grContext.addQuery('state', 'awaiting_trigger');
grContext.query();
return grContext.hasNext();
},
type: 'hr_CaseUtils'
};
Sys ID
24c782869f202200d9011977677fcf89