Temporarily circumventing Business Rules to update Work Notes…..

General Add comments
by:

Recently one of our customers requested a data change. A lot of task related tickets needed to be closed. The customer had some checks build in, to prevent unauthorized status changes. These business rules prevented the tasks to be closed by using a script.
Using the setWorkflow(false) method, the business rules were circumvented. By using this method the Worknotes were no longer updated.
Expecting a lot more data fixes we needed to find a solution to add the work notes to the tickets to make sure the tickets had a reference to the change task that ordered the data change.
The following script include was created, we find the script useful and we are happy to share it here with you.

  • Script type: Script Include
  • Name: createNotes
  • Client Callable: TRUE

[cc lang=”javascript”]
var createNotes = Class.create();

createNotes.prototype = {
initialize : function() {
this.user_id = gs.getUserID();
this.user_name = gs.getUserName();
this.user = gs.getUser();
this.update_time = gs.nowDateTime();
this.modCount = 0;
this.domain = “”;
this.table_name = “”;
this.table_sys_id = “”;
this.note = “”;
},
setTableRecord : function(table_name, table_sys_id) {
this.table_name = table_name.toString();
this.table_sys_id = table_sys_id.toString();
},
getTableRecord : function() {
var gr = new GlideRecord(this.table_name);
if(gr.get(this.table_sys_id)) {
this.modCount = gr.sys_mod_count;
this.domain = gr.sys_domain;
return true;
} else {
return false;
}
},
createNote : function(note) {
this.note = note.toString();
// set modCount and domain
var result = this.getTableRecord();
if(result) {
var jfd = new GlideRecord(‘sys_journal_field’);
jfd.initialize();
jfd.name = this.table_name;
jfd.element = “work_notes”;
jfd.element_id = this.table_sys_id;
jfd.value = this.note;
jfd.insert();

var aud = new GlideRecord(‘sys_audit’);
aud.initialize();
aud.tablename = this.table_name;
aud.documentkey = this.table_sys_id;
aud.fieldname = “work_notes”;
aud.user = this.user_id;
aud.record_checkpoint = this.modCount;
aud.oldvalue = “JOURNAL FIELD ADDITION”;
aud.newvalue = this.note;
aud.insert();
var hst = new GlideRecord (‘sys_history_set’);
hst.addQuery(‘id’, this.table_sys_id);
//hst.addQuery(‘table’, this.table_name);

if( this.domain == “global”){
hst.addQuery(‘domain’, “”);
}else{
hst.addQuery(‘domain’, this.domain);
}
hst.query();
if(hst.next()){

var hle = new GlideRecord(‘sys_history_line’);
hle.initialize();
hle.audit_sysid = aud.sys_id;
hle.field = “work_notes”;
hle.set = hst.sys_id;

if (hst.internal_checkpoint == “”){
hle.internal_checkpoint = ‘0’;
} else{
hle.internal_checkpoint = hst.internal_checkpoint;
}
hle.label = “Work notes”;
hle[‘new’] = this.note;
hle.user_id = this.user_name;
hle.user = this.user_id;
hle.user_name = this.user.getFullName();
hle.update_time = this.update_time;
hle.update = this.modCount;
hle.insert();
return true;
}
} else {
return “Error: Wrong table name and/or table sys id”;
}
}
}
[/cc]
How to call this Script Include in a Background Script
[cc lang=”javascript”]
var test = new createNotes();
test.setTableRecord(“task”, ““);
var result = test.createNote(““);
gs.print(result);
[/cc]

If you have question you can reach me via email on .img[at].img.

One Response to “Temporarily circumventing Business Rules to update Work Notes…..”

  1. Amaury Says:

    Hello sir,

    Thank you for sharing this it works fine !

    Have a great day

Leave a Reply