Update Work notes and Comments between records

Robbie Lacivita
Tera Guru

Good afternoon,

I have a new table that I've built, Knowledge Tasks. This is an extension of the task table, and I have UI actions that create new knowledge tasks from incident, problem, etc.

What I would like to do now is copy work notes and comments between the records. If a comment or work note gets added to the source record, update on the knowledge task, and if a comment or work note is added to the knowledge task, update the source record.

I wrote the below After Update business rule running on the knowledge task table to update the incident record, but it does not work:

copyToIncident();

function copyToIncident() {
	var commentsChanges = current.comments.changes();
	var workNotesChanges = current.work_notes.changes();
	var watchListChanges = current.watch_list.changes();
	var workListChanges = current.work_notes_list.changes();
	
	var link = new GlideRecord("incident");
	link.addQuery("sys_id", current.u_inc_source_request);
	link.query();
	var ids = [];
	while (link.next()) {
		if (commentsChanges)
			link.comments = current.comments.getJournalEntry(1);
		if (workNotesChanges)
			link.work_notes = current.work_notes.getJournalEntry(1);
		if(watchListChanges)
			link.watch_list = current.watch_list;
		if(workListChanges)
			link.work_notes_list = current.work_notes_list;
		link.setWorkflow(false);
		link.update();
	}
}

Do you have any suggestions on how I could fix this?

Thanks!

Robbie

1 ACCEPTED SOLUTION

Robbie Lacivita
Tera Guru

I have this working now, below is the code I used:

 

updateIncident();
updateProblem();
updatePrivateTask();

function updateIncident() {
	var rec = new GlideRecord('incident');
	rec.addQuery('knowledge task',rec.u_knowledge_task);
	rec.addActiveQuery();
	rec.query();
	while (rec.next()) {
		if( rec.getDisplayValue() == current.u_inc_source_request.getDisplayValue()){
			if(current.work_notes.changes()){
				rec.work_notes = 'Work notes copied from knowledge task: '+ current.work_notes.getJournalEntry(1);
			}
			if(current.comments.changes()){
				rec.comments = 'Comments copied from knowledge task: '+ current.comments.getJournalEntry(1);
			}
			if(current.watch_list.changes()){
				rec.watch_list = current.watch_list;
			}
			if(current.work_notes_list.changes()){
				rec.work_notes_list = current.work_notes_list;
			}
			rec.update();
		}
	}
}


function updateProblem() {
	var rec = new GlideRecord('problem');
	rec.addQuery('knowledge task',rec.u_knowledge_task);
	rec.addActiveQuery();
	rec.query();
	while (rec.next()) {
		if( rec.getDisplayValue() == current.u_prob_source_request.getDisplayValue()){
			if(current.work_notes.changes()){
				rec.work_notes = 'Work notes copied from knowledge task: '+ current.work_notes.getJournalEntry(1);
			}
			if(current.comments.changes()){
				rec.comments = 'Comments copied from knowledge task: '+ current.comments.getJournalEntry(1);
			}
			if(current.watch_list.changes()){
				rec.watch_list = current.watch_list;
			}
			if(current.work_notes_list.changes()){
				rec.work_notes_list = current.work_notes_list;
			}
			rec.update();
		}
	}
}

function updatePrivateTask() {
	var rec = new GlideRecord('vtb_task');
	rec.addQuery('knowledge task',rec.u_knowledge_task);
	rec.addActiveQuery();
	rec.query();
	while (rec.next()) {
		if( rec.getDisplayValue() == current.u_private_task.getDisplayValue()){
			if(current.work_notes.changes()){
				rec.work_notes = 'Work notes copied from knowledge task: '+ current.work_notes.getJournalEntry(1);
			}
			if(current.comments.changes()){
				rec.comments = 'Comments copied from knowledge task: '+ current.comments.getJournalEntry(1);
			}
			if(current.watch_list.changes()){
				rec.watch_list = current.watch_list;
			}
			if(current.work_notes_list.changes()){
				rec.work_notes_list = current.work_notes_list;
			}
			rec.update();
		}
	}
}

View solution in original post

6 REPLIES 6

sachin_namjoshi
Kilo Patron
Kilo Patron

You will have to do like below

 

var notes = current.work_notes.getJournalEntry(-1);
//gets all journal entries as a string where each entry is delimited by '\n\n'
var na = notes.split("\n\n");

//stores each entry into an array of strings
 for (var i = 0; i < na.length; i++)                 
  gs.print(na[i]);

 

REgards,

Sachin

Sachin,

 

I apologize, I'm not sure I follow how to translate that into the script that I wrote. Can you provide some further guidance?

 

Thanks,

Robbie

Xavier Cordero
Kilo Sage

Hello Robbie,

I don't believe you need to script for this. Simply add the field from the parent record into the Knowledge Task form. You can dot walk when adding fields on a form. To do this within Knowledge Tasks, simply click on Configure > Form Layout click on your parent (or it seems that in your case it would be source record) field and expand the fields on the parent by clicking on the icon to the top of the arrows in the middle of the screen, locate the work notes from the parent and add them to your form. That way, the actual field on both locations is the field belonging to your incident etc. so it's always up to date and you're not duplicating information within the database.find_real_file.png

First, let me say I didn't realize you could do this, that's really handy!

But, I don't think it meets my requirements exactly. I'm creating knowledge tasks from Incident, Problem, Change tasks, and private tasks. So I have to sync comments and work notes between all of these sources. Can I do this with a parent field like this?

Thanks,

Robbie