Name
sn_hr_core.migrateESignTasks
Description
Migrate Esignature tasks to use new plugin
Script
var HR_TASK_TABLE = "sn_hr_core_task";
var SIGN_TYPE_CREDENTIAL = "credential";
var SIGN_TYPE_SIGNATURE = "signature";
var DOCUMENT_TYPE_MANAGED_DOCUMENT = "managed_document";
var DOCUMENT_TYPE_DOCUMENT_TEMPLATE = "document_template";
var migrateESignTasks = Class.create();
migrateESignTasks.prototype = {
initialize: function() {
},
migrateToEsign : function(){
var processStartTime = new GlideDateTime(new GlideDate());
var startTime = processStartTime;
this.migrateHRTaskTemplates();
gs.info("Time taken for migrating HR Task Templates::"+this._getDuration(startTime));
startTime = new GlideDateTime(new GlideDate());
this._updateTaskData();
gs.info("Time taken for updating task data::"+this._getDuration(startTime));
this._postMigrationSetup();
gs.info("Time taken for migration::"+this._getDuration(processStartTime));
},
_postMigrationSetup : function(){
gs.setProperty('sn_hr_core.isESignatureDataMigrated',true);
this._resetTaskTypeOptionsPostMigration();
},
_resetTaskTypeOptionsPostMigration : function(){
var hrMigrationUtil = new global.HRMigrationUtil();
var encodedQry = '';
var setFields = {};
encodedQry += 'name=' + HR_TASK_TABLE;
encodedQry += '^element=' + 'hr_task_type' ;
encodedQry += '^valueIN' + 'credential,e_signature,sign_document' ;
setFields.inactive = true;
hrMigrationUtil.updateGlobalRecord('sys_choice', encodedQry, setFields);
encodedQry = '';
setFields = {};
encodedQry += 'name=' + HR_TASK_TABLE;
encodedQry += '^element=' + 'hr_task_type' ;
encodedQry += '^value=' + 'e_sign' ;
setFields.inactive = false;
hrMigrationUtil.updateGlobalRecord('sys_choice', encodedQry, setFields);
},
_getDuration : function(startTime){
var currentTime = new GlideDateTime(new GlideDate());
var dur = GlideDateTime.subtract(startTime,currentTime);
return dur.getDisplayValue();
},
/* Migrate open esignature tasks to new plugin
*/
_updateTaskData : function(){
var inQuery=[hr_Constants.TASK_DRAFT,hr_Constants.TASK_QUALIFIED,hr_Constants.TASK_WORK_IN_PROGRESS];
var gr = new GlideRecord(HR_TASK_TABLE);
gr.addQuery('hr_task_type', 'IN', 'e_signature,sign_document,credential');
gr.addQuery('state','IN',inQuery);
gr.query();
var signType;
var documentType;
var configId;
while(gr.next()){
if(gr.hr_task_type=='credential')
signType = SIGN_TYPE_CREDENTIAL;
else
signType = SIGN_TYPE_SIGNATURE;
if(gr.hr_task_type=='sign_document')
documentType = DOCUMENT_TYPE_DOCUMENT_TEMPLATE;
else
documentType = DOCUMENT_TYPE_MANAGED_DOCUMENT;
var config = new GlideRecord("sn_esign_configuration");
config.addQuery('task_table',HR_TASK_TABLE);
config.addQuery('document_type',documentType);
config.addQuery('signature_type',signType);
if(gr.hr_task_document && documentType == 'managed_document')
config.addQuery(DOCUMENT_TYPE_MANAGED_DOCUMENT,gr.hr_task_document);
else
config.addNullQuery(DOCUMENT_TYPE_MANAGED_DOCUMENT);
if(gr.acknowledgment_text)
config.addQuery('acknowledgement_text',gr.acknowledgment_text);
else
config.addNullQuery('acknowledgement_text');
config.query();
if(!config.next())
configId = this._createConfiguration(signType,documentType,gr.hr_task_document,gr.acknowledgment_text);
else
configId = config.getUniqueValue();
gr.hr_task_type = 'e_sign';
gr.sn_esign_esignature_configuration=configId;
gr.setWorkflow(false);
gr.update();
}
},
/* Migrate Esignature HR task templates to use new esignature plugin functionality
*/
migrateHRTaskTemplates : function(){
var signType;
var docType;
var managedDocument;
var ackText;
var configId;
var modifiedTemplate;
var name;
var gr = new GlideRecord("sn_hr_core_template");
gr.addQuery('table', HR_TASK_TABLE);
var con1 = gr.addQuery('template','CONTAINS','hr_task_type=e_signature');
con1.addOrCondition('template','CONTAINS','hr_task_type=credential');
con1.addOrCondition('template','CONTAINS','hr_task_type=sign_document');
gr.query();
while(gr.next()){
var templateString = gr.getValue("template");
var templateValues = this._getTemplateValues(templateString);
if(templateValues['hr_task_type']=='credential')
signType = SIGN_TYPE_CREDENTIAL;
else
signType = SIGN_TYPE_SIGNATURE;
if(templateValues['hr_task_type']=='sign_document')
docType = DOCUMENT_TYPE_DOCUMENT_TEMPLATE;
else
docType = DOCUMENT_TYPE_MANAGED_DOCUMENT;
ackText = templateValues['acknowledgment_text'];
managedDocument = templateValues['hr_task_document'];
configId = this._getConfiguration(signType,docType,managedDocument,ackText);
if(!configId)
configId = this._createConfiguration(signType,docType,managedDocument,ackText);
modifiedTemplate = this._getMigratedTemplate(templateString,templateValues,configId);
gr.template = modifiedTemplate;
gr.update();
}
},
/* Get Managed Document Name for given Document.
* @param managed Document sys id
* @return string name of the managed document
*/
_getManagedDocumentName : function(managedDocument){
var name = "";
var document = new GlideRecord("dms_document");
document.get(managedDocument);
if(document)
name = document.name;
return name;
},
/* Get Migrated Template string
* @param old template String
* @param old template values array
* @param configuration id
* @return string migrated template String
*/
_getMigratedTemplate : function(templateString, templateValues, configId){
var updatedTemplate = templateString;
var condition;
if(templateValues['hr_task_type'])
condition = 'hr_task_type='+templateValues['hr_task_type'];
updatedTemplate = updatedTemplate.replace(condition,"hr_task_type=e_sign");
if(templateValues['acknowledgment_text'] && configId){
condition = 'acknowledgment_text='+templateValues['acknowledgment_text']+'^';
updatedTemplate = updatedTemplate.replace(condition,"");
}
if(templateValues['hr_task_document']){
condition = 'hr_task_document='+templateValues['hr_task_document']+'^';
updatedTemplate = updatedTemplate.replace(condition,"");
}
if(configId)
updatedTemplate = 'sn_esign_esignature_configuration='+configId+'^'+updatedTemplate;
return updatedTemplate;
},
/* Get Template values for given template String
* @param template String
* @return array template values array
*/
_getTemplateValues : function(templateString){
var template = {};
var splitListFields = templateString.split("^");
for(i=0;i<splitListFields.length&&splitListFields[i]!=='EQ';i++)
{
var seperateFiled_Values = splitListFields[i].split("=");
template[seperateFiled_Values[0]] =seperateFiled_Values[1];
}
return template;
},
/* Get configuration for given signature type, document type and document
* @param string signature type
* @param string document type
* @param managed document sys id
* @param string acknowledgement text
* @return configuration sys id
*/
_getConfiguration : function(signType,docType,managedDocument,ackText){
var configId;
var config = new GlideRecord("sn_esign_configuration");
config.addQuery('task_table', HR_TASK_TABLE);
config.addQuery('signature_type', signType);
if(managedDocument && docType == DOCUMENT_TYPE_MANAGED_DOCUMENT)
config.addQuery(DOCUMENT_TYPE_MANAGED_DOCUMENT, managedDocument);
if(ackText)
config.addQuery('acknowledgement_text', ackText);
else
config.addNullQuery('acknowledgement_text');
config.addQuery('document_type', docType);
config.query();
if(config.next()){
configId = config.getUniqueValue();
}
return configId;
},
/* Create configuration for given signature type, document type and document
* @param string signature type
* @param string document type
* @param managed document sys id
* @param string acknowledgement text
* @return configuration sys id
*/
_createConfiguration : function(signType,docType,managedDocument,ackText){
var name;
if(managedDocument && docType==DOCUMENT_TYPE_MANAGED_DOCUMENT)
name = this._getManagedDocumentName(managedDocument);
else if(docType==DOCUMENT_TYPE_DOCUMENT_TEMPLATE)
name = "Document template";
else
return;
var config = new GlideRecord("sn_esign_configuration");
config.initialize();
config.name = name;
if(ackText)
config.acknowledgement_text = ackText;
if(managedDocument)
config.managed_document = managedDocument;
config.active = true;
config.document_type = docType;
config.signature_type = signType;
config.task_table = HR_TASK_TABLE;
return config.insert();
},
type: 'migrateESignTasks'
};
Sys ID
c55ade2fb7d03300a251e556ee11a9d4