Name
global.CertificationTaskCreate
Description
Creates cert_task record and related cert_element records from cert_schedule (extension of sysauto_script)
Script
var CertificationTaskCreate = Class.create();
CertificationTaskCreate.prototype = {
initialize : function(auditInstance, certDef) {
this.previewOnly = false;
this.outputMsg = "";
this.hasCertTasks = false;
var daysToComplete = 0;
if (typeof auditInstance != "undefined") {
this.auditInstance = auditInstance;
var cai = new GlideRecord("cert_audit_instance");
cai.get(auditInstance);
daysToComplete = cai.cert_audit_definition.days_to_complete;
}
if (typeof certDef != "undefined")
this.certDef = certDef;
else {
this.certDef = current;
daysToComplete = current.days_to_complete;
}
var table = new GlideRecord(this.certDef.table);
this.tableLabel = table.getLabel();
this.tablePlural = table.getPlural();
this.assignType = this.certDef.assignment_type;
if (this.assignType == "Specific User" || this.assignType == "User Field") {
this.assignField = "assigned_to";
this.orderField = this.certDef.assign_to + '.sys_id';
} else {
this.assignField = "assignment_group";
this.orderField = this.certDef.assign_to_group + '.sys_id';
}
this.completeBy = "";
if (daysToComplete > 0)
this.completeBy = gs.daysAgoLocal(0 - daysToComplete);
},
previewCerts: function() {
this.previewOnly = true;
this.runCerts();
return this.hasCertTasks ? this.outputMsg : 'No Certification Tasks would be created';
},
runCerts: function() {
if (this.certDef.getTableName() == 'cert_instance')
return;
this.instance = this.createCertificationInstance();
var elementList = this.certDef.fields.split(",");
var user = "xxxx";
var records = 0;
if (!this.previewOnly)
this.updateCertLastRunTime();
this.updateCertNextRunTime();
var gr = new GlideRecord(this.certDef.table);
gr.addEncodedQuery(this.certDef.filter.filter_condition);
if (this.certDef.assign_to_empty == "Do Not Create Task") {
if (this.assignType == "User Field")
gr.addNotNullQuery(this.certDef.assign_to);
else if (this.assignType == "Group Field")
gr.addNotNullQuery(this.certDef.assign_to_group);
}
gr.orderBy(this.orderField);
gr.query();
//If filter doensn't return any records, set the cert instance to close complete.
if (gr.getRowCount() == 0) {
var certInstance = new GlideRecord('cert_instance');
if (certInstance.get(this.instance)) {
certInstance.state = 3;
certInstance.percent_complete = 100;
certInstance.update();
}
}
gs.globalPut("no_update_cert_task", "true");
while (gr.next()) {
var newTask;
var assignee = this.getAssignee(gr);
if (user != assignee) {
if (records == 1)
this.outputMsg += records + " " + this.tableLabel + "<br/>";
else
this.outputMsg += records + " " + this.tablePlural + "<br/>";
newTask = this.createCertificationTask(gr, assignee);
user = assignee.toString();
records = 0;
}
records++;
var cert = this.initCertification(gr, assignee, newTask);
if (elementList.length == 0) {
this.insertCert(cert);
continue;
}
for (var i = 0; i < elementList.length; i++) {
cert.element = elementList[i];
cert.original_value = gr.getDisplayValue(elementList[i]);
this.insertCert(cert);
}
}
if (records == 1)
this.outputMsg += records + " " + this.tableLabel + "<br/>";
else
this.outputMsg += records + " " + this.tablePlural + "<br/>";
this.hasCertTasks = (records == 0) ? false : true;
this.updateBuildingFlag();
return this.instance;
},
updateBuildingFlag: function() {
if (this.previewOnly)
return;
var certInstance = new GlideRecord('cert_instance');
if(certInstance.get(this.instance)) {
certInstance.is_building = false;
certInstance.update();
}
},
updateCertLastRunTime: function(){
this.certDef.last_run_date = gs.nowDateTime();
this.updateCertDef();
},
updateCertNextRunTime: function(isExecutionUpdate){
isExecutionUpdate =
isExecutionUpdate === undefined ? true : (isExecutionUpdate && true);
if (this.certDef.run_type != "on_demand") {
this.certDef.next_scheduled_run = (new CertificationUtils()).getTriggerNextRunTime(isExecutionUpdate, "cert_schedule", this.certDef.sys_id);
this.updateCertDef();
return;
}
this.certDef.next_scheduled_run = "";
this.updateCertDef();
},
updateCertDef: function(){
// turn off business rules to prevent recursive behavior
this.certDef.setWorkflow(false);
this.certDef.update();
// turn business rules back on
this.certDef.setWorkflow(true);
},
insertCert: function(cert) {
if (!this.previewOnly) {
cert.insert();
return;
}
},
initCertification: function(gr, assignee, certTaskID) {
var cert = new GlideRecord('cert_element');
var assignment_type = this.getAssignmentType();
if(assignment_type.equals("user"))
cert.assigned_to = assignee;
cert.comments = 'Certify your ' + gr.getLabel() + ': ' + gr.getDisplayValue();
cert.cert_task = certTaskID;
cert.table = this.certDef.table;
cert.id = gr.sys_id;
return cert;
},
createCertificationTask: function(gr, assignee) {
if (assignee == '' && this.certDef.assign_to_empty == "Create Assigned Task") {
if (this.certDef.assignment_type == "User Field")
assignee = this.certDef.user;
else if (this.certDef.assignment_type == "Group Field")
assignee = this.certDef.group;
}
var ctask = new GlideRecord('cert_task');
// if assignee already has an active task, return sys_id
ctask.addActiveQuery();
ctask.addQuery("cert_schedule", this.certDef.sys_id);
ctask.addQuery("cert_instance", this.instance);
ctask.addQuery(this.assignField, assignee);
ctask.query();
if (ctask.next())
return ctask.sys_id;
ctask.initialize();
ctask.cert_schedule = this.certDef.sys_id;
ctask.cert_instance = this.instance;
ctask[this.assignField] = assignee;
if (this.certDef.task_description.nil())
ctask.short_description = 'Certify your ' + this.tablePlural;
else
ctask.short_description = this.certDef.task_description;
ctask.complete_by = this.completeBy;
return this.insertCertTask(ctask);
},
insertCertTask: function(ctask) {
if (!this.previewOnly)
return ctask.insert();
if (!ctask.assigned_to.nil())
this.outputMsg += "One Certification Task would be created for " + ctask.assigned_to.getDisplayValue() + " to certify ";
else
this.outputMsg += "One Certification Task would be created unassigned to certify ";
return "";
},
createCertificationInstance: function() {
if (this.previewOnly)
return "";
var instance = new GlideRecord('cert_instance');
instance.cert_schedule = this.certDef.sys_id;
instance.table = this.certDef.table;
instance.fields = this.certDef.fields;
instance.display_fields = this.certDef.display_fields;
instance.cert_condition = this.certDef.cert_condition;
instance.instructions = this.certDef.instructions;
instance.cert_audit_instance = this.auditInstance;
instance.complete_by = this.completeBy;
instance.is_building = true;
var instanceID = instance.insert();
var sl = new GlideSysList(this.certDef.table.toString());
sl.setViewName("RPT" + instanceID);
sl.save(this.certDef.display_fields + ',' + this.certDef.fields);
return instanceID;
},
getAssignee: function(gr) {
if (this.assignType == "Specific User")
return this.certDef.user.toString();
else if (this.assignType == "Specific Group")
return this.certDef.group.toString();
else if (this.assignType == "User Field")
return gr.getElement(this.certDef.assign_to);
else if (this.assignType == "Group Field")
return gr.getElement(this.certDef.assign_to_group);
},
getAssignmentType: function() {
if (this.assignType == "Specific User")
return "user";
else if (this.assignType == "Specific Group")
return "group";
else if (this.assignType == "User Field")
return "user";
else if (this.assignType == "Group Field")
return "group";
},
type: 'CertificationTaskCreate'
};
Sys ID
54da7f860a0a3c1e01d1b209ac82b48a