Find your people. Pick a challenge. Ship something real. The CreatorCon Hackathon is coming to the Community Pavilion for one epic night. Every skill level, every role welcome. Join us on May 5th and learn more here.

BobDarroch
ServiceNow Employee

I spent a bunch of time trying to find a way to add data to journal fields while maintaining the original time stamp and user ID for a customer migrating to ServiceNow and wanting to maintain their notes from their existing system.   I suspect that this issue has been solved a number of times, however I didn't see it posted anywhere.  Here is the code that I used to make this happen, this is based on my experience & no guarantees 

 

insertJournalHistory: function(targetRecord, content, userName, createdDateTime) {
this.updateNumber = 1;
this.tableName = targetRecord.sys_class_name;
this.sysID = targetRecord.sys_id.toString()
this.createdDateTime = new GlideDateTime();
this.createdDateTime.setDisplayValue(createdDateTime,"MM-dd-yy HH:mm:ss");
this.journalField = new GlideRecord("sys_journal_field");
this.journalField.element = "comments";
this.journalField.element_id = this.sysID;
this.journalField.name = this.tableName;
this.journalField.sys_created_by = userName;
this.journalField.sys_created_on = this.createdDateTime;
this.journalField.value = content;
this.journalField.autoSysFields(false);
this.journalSysID = this.journalField.insert();
this.historySet = new GlideRecord("sys_history_set");
this.historyExists = this.historySet.get("id", this.sysID);
if (this.historyExists) {
this.historyID = this.historySet.sys_id.toString();
this.historySet.updates += 1;
this.historySet.update();
this.updateNumber = this.historySet.updates;
} else {
this.historySet.date_format = "yyyy-MM-dd HH:mm:ss";
this.historySet.id = this.sysID;
this.historySet.language = "en";
this.historySet.last_update_recorded = this.createdDateTime;
this.historySet.line_table = "sys_history_line0000";
this.historySet.sys_created_by = "system";
this.historySet.sys_created_on = this.createdDateTime;
this.historySet.sys_updated_by = "system";
this.historySet.sys_updated_on = this.createdDateTime;
this.historySet.table = this.tableName;
this.historySet.timezone = "America/Los_Angeles";
this.historySet.autoSysFields(false);
this.historyID = this.historySet.insert();
}
this.historyLine = new GlideRecord("sys_history_line");
this.historyLine.field = "comments";
this.historyLine.journal_sysid = this.journalSysID;
this.historyLine.label = "Additional comments";
this.historyLine.setValue("new", content);
this.historyLine.setValue("set", this.historyID);
this.historyLine.sys_created_on = this.createdDateTime;
this.historyLine.sys_updated_on = this.createdDateTime;
this.historyLine.update_time = this.createdDateTime;
this.historyLine.type = "audit";
this.historyLine.user_name = userName;
this.historyLine.update = this.updateNumber;
this.historyLine.autoSysFields(false);
this.historyLineID = this.historyLine.insert();
new GlideHistorySet(this.tableName, this.sysID);
},
1 Comment