How do you script a historical comment into the work notes?

bradwestvig
Giga Guru

We have a custom table that was created by our implementation partner to store comments.  I want to move those comments into the work_notes and comments field on the incident table. 

I wrote a test script to test this, but it doesn't seem to show up.

var gr = new GlideRecord('sys_journal_field');
gr.element = 'work_notes';
gr.element_id = <sys_id>;
gr.name = 'incident';
gr.sys_created_by = <user_id>;
gr.value = 'some notes';

gr.insert();
1 ACCEPTED SOLUTION

After reviewing with ServiceNow, it is a system control to not allow for back dating or doing historical notes on behalf of someone.  We decided to go ahead and run the script as system, and then insert the date the original comment was added with the username.

View solution in original post

8 REPLIES 8

Sure do share us the updates.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I'm still waiting for an answer to my question from Support.  I have found a way to get comments added by using a script like this.  I got this idea from another article in the community, so I can't take credit.  This works, but it doesn't get me the timestamp that I wanted.  I think this ok, since really it shows you can't add comments in the past for any activities. Probably a good measure for auditing.

function gdtToCheckpoint(){
    var gdt = new GlideDateTime();
		var n = gdt.getNumericValue();
		return n.toString(16)+"0000001";
	}

		var reqActHistory = new GlideRecord("custom_table");
		reqActHistory.query();

		// Set the update count to the current mod count
		var count = parseInt(targetGR.getValue("sys_mod_count"));
		
		while(reqActHistory.next()){
			// Create a new sys audit record for the update
			var auditGR = new GlideRecord("sys_audit");
			auditGR.documentkey = targetGR.getUniqueValue();
			auditGR.fieldname = 'work_notes'
			auditGR.newvalue = reqActHistory.body;
			auditGR.oldvalue = "JOURNAL FIELD ADDITION";
			//auditGR.reason = "Transfer from " + origGR.getDisplayValue();
			auditGR.record_checkpoint = count;
			auditGR.internal_checkpoint = this._gdtToCheckpoint(new GlideDateTime(journGR.sys_created_on));
			auditGR.tablename = 'target_table'

			// Set the audit user information information
			auditGR.sys_created_by = reqActHistory.sys_created_by;
			auditGR.user = reqActHistory.sys_created_by;
			
			// Set the time to current because audit history doesn't look for any audits created before the record
			// which means that if you are transferring an older ticket to a new one the worknotes and comments
			// will not show up
			auditGR.sys_created_on = new GlideDateTime();
			auditGR.newvalue = "Original note time " + reqActHistory.sys_created_on + "\n" + reqActHistory.newvalue;
			
			// Create the record with no automatic system fields so that sys_created_on and sys_created_by are 
			// not overwritten
			auditGR.autoSysFields(false);
			auditGR.insert();

			// Create the journal record for the new record
			journGR.name = targetGR.sys_class_name;
			journGR.element_id = targetGR.getUniqueValue();
			var id = journGR.insert();
			
			// Update the "sys_created_by" to be the original commenting user
			var newJournGR = new GlideRecord("sys_journal_field");
			newJournGR.get(id);
			newJournGR.sys_created_by = auditGR.sys_created_by;
			newJournGR.update();
			
			count = count + 1;
		}

		// Make the target count each of the notes or comments as an update
		targetGR.sys_mod_count = count;
		targetGR.autoSysFields(false);
		targetGR.update();
	

Hi,

I hope ServiceNow responds with some workaround since you already raised a Case with them.

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

After reviewing with ServiceNow, it is a system control to not allow for back dating or doing historical notes on behalf of someone.  We decided to go ahead and run the script as system, and then insert the date the original comment was added with the username.