Created
April 8, 2014 13:17
-
-
Save paulsena/10122701 to your computer and use it in GitHub Desktop.
Service Now Helper Script to Syncronize the Journal fields between Instances. If you can, it is recommended to just sync the text using getJournalEntry. This script will replicate all Activity log history, Audit records, and Journal table entries so it looks like it originated from the target instance.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Name: JournalSyncHelper | |
* Description: Helper class for Masco Milgard 2 way sync. Allows Journal fields (Comments, Worknotes) to be synced. | |
* Helper functions to package up Comments and WorkNotes to a JSON String. Also function to decode and insert | |
* packaged journal field back in SN. | |
* Author: Paul.Senatillaka@fruitionpartners.com | |
* | |
* Version: 2.0 - 10/11/13 - Rewrite from v1 to better handle large comments and numerous other fixes. | |
*/ | |
gs.include("PrototypeServer"); | |
var JournalSyncHelper = Class.create(); | |
JournalSyncHelper.prototype = { | |
initialize: function () {}, | |
/* | |
* Package up Journal entries (Comments, Worknotes) into a JSON Object for tramissions over WS. | |
* Function used by sending system. | |
*/ | |
packageJournal: function (recordId, includeWorkNotes) { | |
try { | |
var grJournal = new GlideRecord('sys_journal_field'); | |
if (includeWorkNotes) { | |
//Query for Unsynced Worknotes or Comments for a given Record | |
grJournal.addEncodedQuery("element_id=" + recordId + "^u_synced=false^element=work_notes^ORelement=comments"); | |
} else { | |
//Query for just Unsynced Comments for a given Record | |
grJournal.addEncodedQuery("element_id=" + recordId + "^u_synced=false^element=comments"); | |
} | |
grJournal.orderByDesc('sys_created_on'); | |
grJournal.query(); | |
var packedJournal = []; | |
var json = new JSON(); | |
while (grJournal.next()) { | |
//Lookup Journal User | |
var grUser = GlideRecord('sys_user'); | |
grUser.get('user_name', grJournal.sys_created_by); | |
//Truncate extremely large journal entries | |
var journalValue = grJournal.value.toString(); | |
if (journalValue.length > 20000) { | |
journalValue = journalValue.substr(0,19960) + "\r\n\r\n[Message Truncated to 20K Characters]"; | |
} | |
//Create object for JSON Array | |
var entry = { | |
element: grJournal.element.toString(), | |
value: JSUtil.escapeText(journalValue), | |
sys_created_by: grJournal.sys_created_by.toString(), | |
sys_created_on: grJournal.sys_created_on.toString(), | |
name: grUser.name.toString() | |
}; | |
packedJournal.push(entry); | |
//Marked Journal entry as Synced so we don't grab it next time! | |
grJournal.u_synced = "true"; | |
grJournal.update(); | |
} | |
return json.encode(packedJournal); | |
} | |
catch (err) { | |
gs.logError('Failed Packaging Journal fields for Incident: ' + recordId + " \n Error Msg: " + err.toString(), "Record Sync"); | |
} | |
}, | |
/* | |
* Unpackage JSON Journal object and insert into Journal Table. Also insert into Audit, and History Line table | |
* so entries show up correctly in the Activity Feed. | |
* Function used by reciving system. | |
*/ | |
syncJournal: function (recordId, jsonJournal) { | |
try { | |
//Write to System Log | |
gs.log('Starting sync of Journal Fields for Incident: ' + recordId, "Record Sync"); | |
var json = new JSON(); | |
var journal = json.decode(jsonJournal); | |
//Lookup History Set. We need this to insert Audit and History Line (Activity) Records | |
var grHistorySet = new GlideRecord('sys_history_set'); | |
if ( !grHistorySet.get('id', recordId) ) { | |
gs.log('Debug: History Set not found for Incident: ' + recordId, "Record Sync"); | |
} | |
//Query Journal Field | |
var grJournal = new GlideRecord('sys_journal_field'); | |
grJournal.addEncodedQuery("element_id=" + recordId + "^element=work_notes^ORelement=comments"); | |
grJournal.orderByDesc('sys_created_on'); | |
grJournal.query(); | |
//Loop through source Journal entries | |
for (var i = 0; i < journal.length; i++) { | |
//Check if Journal entry Exists | |
if ( !(grJournal.find('value', journal[i].value) && grJournal.sys_create_on == journal[i].sys_created_on) ) { | |
//Write to Import Set Log | |
log.info('Creating ' + journal[i].element + ' for: ' + journal[i].value); | |
//Insert Journal | |
var grNewJournal = new GlideRecord('sys_journal_field'); | |
grNewJournal.initialize(); | |
grNewJournal.name = 'task'; | |
grNewJournal.element_id = recordId; | |
grNewJournal.element = journal[i].element; | |
grNewJournal.value = journal[i].value; | |
grNewJournal.sys_created_on = journal[i].sys_created_on; | |
grNewJournal.sys_created_by = journal[i].sys_created_by; | |
grNewJournal.u_synced = true; //So we don't sync back later | |
grNewJournal.autoSysFields(false); //So Created On and By don't auto populate | |
grNewJournal.insert(); | |
//Insert Audit | |
var grAudit = new GlideRecord('sys_audit'); | |
grAudit.initialize(); | |
grAudit.documentkey = recordId; | |
grAudit.internal_checkpoint = grHistorySet.internal_checkpoint; | |
grAudit.tablename = 'incident'; | |
grAudit.fieldname = journal[i].element; | |
grAudit.newvalue = journal[i].value; | |
grAudit.oldvalue = "JOURNAL FIELD ADDITION"; | |
grAudit.user = journal[i].sys_created_by; | |
grAudit.sys_created_by = journal[i].sys_created_by; | |
grAudit.sys_created_on = journal[i].sys_created_on; | |
grNewJournal.autoSysFields(false); //So Created On and By don't auto populate | |
grAudit.insert(); | |
//Insert History Line | |
var grNewActvity = new GlideRecord('sys_history_line'); | |
grNewActvity.initialize(); | |
grNewActvity.set = grHistorySet.sys_id; | |
grNewActvity.audit_sysid = grAudit.sys_id; | |
grNewActvity.field = journal[i].element; | |
grNewActvity['new'] = journal[i].value; | |
if (journal[i].element == 'comments') { | |
grNewActvity.label = 'Synced Additional Comments'; | |
} else if (journal[i].element == 'work_notes') { | |
grNewActvity.label = 'Synced Work Notes'; | |
} | |
grNewActvity.user_id = journal[i].sys_created_by; | |
grNewActvity.user = journal[i].sys_created_by; | |
grNewActvity.user_name = journal[i].name; | |
grNewActvity.update_time = journal[i].sys_created_on; | |
grNewActvity.internal_checkpoint = grHistorySet.internal_checkpoint; | |
grNewJournal.autoSysFields(false); //So Created On and By don't auto populate | |
grNewActvity.insert(); | |
//Write to System Log | |
gs.log('Successfully synced journal entry: ' + grNewJournal.sys_id + ' by:' + grNewJournal.sys_created_by + ' for Incident: ' + recordId, "Record Sync"); | |
} | |
} | |
//Update History | |
if (typeof GlideHistorySet != 'undefined') | |
GlideHistorySet(grHistorySet).refresh(); | |
else | |
Packages.com.glide.audit.HistorySet(grHistorySet).refresh(); | |
return true; | |
} | |
catch(err) { | |
gs.logError('Failed Parsing and Inserting Journal fields for Incident: ' + recordId + " \n Error Msg: " + err.toString(), "Record Sync"); | |
return false; | |
} | |
} | |
}; |
Yes they should, though unfortunately this hasn't solved my issue in Geneva with the activity feed showing the incorrect date.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
New to this - lines 131 and 151 reference grNewJournal, shouldn't they reference grAudit / grNewActivity?