Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

copy work notes from problem to problem_task using business rule and vice versa

tghadage124
Tera Contributor

Hi guys , 

 

(function executeRule(current, previous /*null when async*/) {

    // Initialize GSLog for logging
    var gl = new GSLog("com.uber.problem_sc_task.log", "uber.ProblemScTask");

    // To Copy Work Notes from Parent Problem to Child Tasks and Vice Versa
    var compare, parent_work_note, task_work_note, work_note_from_parent;

    // Get the work notes from the parent (Problem) record
    parent_work_note = current.work_notes.getJournalEntry(1).match(/\n.*/gm).join('').replace(/^\s*\n/gm, "");
    work_note_from_parent = parent_work_note.toString().trim();

    gl.logDebug('[KPMG_CopyWorkNotesProblemToSCTASK] Work Note added to Problem ' + current.number + ': ' + work_note_from_parent);

    // GlideRecord for fetching child tasks (sc_task) linked to the parent problem record
    var task_gr = new GlideRecord('problem_task');
    task_gr.addQuery('parent', current.sys_id);  // 'parent' field links tasks to the parent problem
    task_gr.query();

    // Verify that the work note did not originate from a catalog task (to avoid redundant copying)
    while (task_gr.next()) {
        if (task_gr.work_notes.getJournalEntry(1)) {

            task_work_note = task_gr.work_notes.getJournalEntry(1).match(/\n.*/gm).join('').replace(/^\s*\n/gm, "");
            task_work_note = task_work_note.toString().trim();

            compare = work_note_from_parent.indexOf(task_work_note);
            if (compare > -1) // If a match is found, work note is already present in the task
            {
                gl.logDebug('[KPMG_CopyWorkNotesProblemToSCTASK] This work note was added to Problem ' + current.number + ' due from catalog task ' + task_gr.number);
                return;
            }
        }
    }

    // Rerun the task query to copy work notes to each catalog task linked to the Problem
    task_gr.query();
    while (task_gr.next()) {
        if (task_gr.work_notes.getJournalEntry(1)) {

            task_work_note = task_gr.work_notes.getJournalEntry(1).match(/\n.*/gm).join('').replace(/^\s*\n/gm, "");
            task_work_note = task_work_note.toString().trim();

            gl.logDebug('[KPMG_CopyWorkNotesProblemToSCTASK] Task work note for ' + task_gr.number + ': ' + task_work_note);

            // Compare parent work note with task work note, and copy if no match is found
            compare = work_note_from_parent.indexOf(task_work_note);
            if (compare == -1) // If no match found
            {
                task_gr.work_notes = work_note_from_parent.trim();  // Copy parent work note to task work notes
                task_gr.update();

                gl.logDebug('[KPMG_CopyWorkNotesProblemToSCTASK] Work note added to catalog task ' + task_gr.number + ' from Problem ' + current.number + ': ' + work_note_from_parent);
            }
        } else {
            // If the task has no work note, simply add the parent work note
            task_gr.work_notes = work_note_from_parent.trim();
            task_gr.update();
        }
    }

})(current, previous);  
 
its not working as expected. 
please help!!
1 REPLY 1

Brad Bowman
Kilo Patron
Kilo Patron

This can be a bear.  Here is at least the second iteration of what we've now been using in Production for years.  I remember there were modifications for the target record having and not having any previous entry, and users repeatedly entering the same note for some reason. We have the same for Additional comments.  This example is on the RITM and Catalog Task, but you'll get the idea.

Business Rule 1: sc_req_item table, after update, Work notes changes, Updated by is not system:

(function executeRule(current, previous /*null when async*/) {
	//To Copy the Work notes from RITM to SCTASKs
    var compare = '';
	var ritm_wn2 = '';
	var ritm_wn = '';
	var task_wn2 = '';
	var task_wn = '';
	
	ritm_wn = current.work_notes.getJournalEntry(1);
	
	//Remove timestamp and name from work note
	var regex= new RegExp('\n');
	var i = ritm_wn.search(regex);
	if (i>0) {
		ritm_wn2 = ritm_wn.substring(i+1, ritm_wn.length);	
	}
	
	var task_gr = new GlideRecord('sc_task');
	task_gr.addQuery('request_item', current.sys_id);
	task_gr.addQuery('active', 'true');
	task_gr.query();
	while (task_gr.next()) {
		task_wn = task_gr.work_notes.getJournalEntry(1);
		
		//Remove timestamp and name from work note
		var i1 = task_wn.search(regex);
		if(i1 > 0) {
			task_wn2 = task_wn.substring(i1+1, task_wn.length);	
			var numberstring = current.number + ' - ';
			var regex2= new RegExp(numberstring);
			var i2 = task_wn2.search(regex2);
			if(i2>=0) {
				task_wn2 = task_wn2.substring(i2+numberstring.length, task_wn2.length);
			}
		} else {
			task_wn2 = 'empty';
		}
		
		var numberstring2 = task_gr.number + ' - ';
		var regex3= new RegExp(numberstring2);
		var i3 = ritm_wn2.search(regex3);
		if(i3>=0) {
			ritm_wn2 = ritm_wn2.substring(i3+numberstring2.length, ritm_wn2.length);
		}
		
		compare = ritm_wn2.indexOf(task_wn2);
		
		if (compare != 0) {// if no exact entire match found
			task_gr.work_notes = current.number + ' - ' + ritm_wn2.trim();
			task_gr.update();
		}
	}	
})(current, previous);

 

Business Rule 2: sc_task table, after update, Work notes changes:

(function executeRule(current, previous /*null when async*/) {
	//to copy the Work Notes from SCTASK to RITM
	var compare = '';
	var task_wn2 = '';
	var task_wn = '';
	var ritm_wn2 = '';
	var ritm_wn = '';
	
	task_wn = current.work_notes.getJournalEntry(1);
				
	//Remove timestamp and name from work note
	var regex= new RegExp('\n');
	var i = task_wn.search(regex);
	if (i>0) {
		task_wn2 = task_wn.substring(i+1, task_wn.length);
	}
	
	var ritm_gr = new GlideRecord('sc_req_item');
	ritm_gr.addQuery('sys_id', current.request_item);
	ritm_gr.query();
	if(ritm_gr.next()) {		
		ritm_wn = ritm_gr.work_notes.getJournalEntry(1);
		//Remove timestamp and name from work note
		var i1 = ritm_wn.search(regex);
		if(i1 > 0) {
			ritm_wn2 = ritm_wn.substring(i1+1, ritm_wn.length);
			var numberstring = current.number + ' - ';
			var regex2= new RegExp(numberstring);
			var i2 = ritm_wn2.search(regex2);
			if(i2>=0) {
				ritm_wn2 = ritm_wn2.substring(i2+numberstring.length, ritm_wn2.length);
			}
		} else {
			ritm_wn2 = 'empty';
		}
		
		var numberstring2 = ritm_gr.number + ' - ';
		var regex3= new RegExp(numberstring2);
		var i3 = task_wn2.search(regex3);
		if(i3>=0) {
			task_wn2 = task_wn2.substring(i3+numberstring2.length, task_wn2.length);
		}
	
		compare = task_wn2.indexOf(ritm_wn2);
	
		if(compare != 0) {  //if no EXACT ENTIRE match found
			ritm_gr.work_notes = current.number + ' - ' + task_wn2.trim();
			ritm_gr.update();
		}		
	}
})(current, previous);