business rule doesn't update work notes

Ravish Shetty
Tera Guru

i have this below piece of code in a business rule on sc_task

find_real_file.png

 

business rule conditions are as below 

 

find_real_file.png

 

the business rule intends to copy work notes of one task into all adjacent tasks of the same request item.

the code however doesn't work and i do not get any error. when i printed different messages, it seems all the code works fine except for the part where i am trying to update the work notes. i de-activated all other business rules on task and sc_task and it still doesn't work. 

the execution reaches till line 16.

1 ACCEPTED SOLUTION

Brent Sutton
Mega Sage

Hi Ravish,

I had a similar requirement to copy customer comments from a sc_task to sc_req_item. I had to clean up the comments and compare in order to prevent an infinite loop. I've modified the code below which should work for your use case:

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

	var taskSysId = current.getUniqueValue();
	//gs.info(gs.getMessage("BLS: taskSysIdk: {0}",[taskSysId]));

	var trgt = new GlideRecord("sc_task");
	trgt.addQuery("parent",current.parent);
	trgt.addQuery("sys_id","!=",taskSysId);
	trgt.addActiveQuery();
	trgt.query();

	while (trgt.next()) {
		gs.info("BLS: trgt: " + trgt.number);
		// Before we update the target task, we need to prevent an infinite loop by ensuring the work_notes that is causing this business rule to run is not the same text that was in the last work_notes on the target task.

		//return the last work_notes journal entry for the current task and extract the journal text below the line break.
		var workNotes = cleanWorkNotes(current.work_notes.getJournalEntry(1));
		//return the last work_notes journal entry for the Catalog Task and extract the journal text below the line break.
		var trgtWorkNotes = cleanWorkNotes(trgt.work_notes.getJournalEntry(1));

		gs.info(gs.getMessage("BLS: Current Task: {0} >>> Current Task Work Notes: {1} - Target Task: {2} >>> trgtWorkNotes: {3} indexOf: {4}",[current.number,workNotes,trgt.number, trgtWorkNotes,trgtWorkNotes.indexOf(workNotes)]));

		/***** NOTE: If the same work_notes are entered twice, the second journal entry will not be copied to the target task as it will match the first work_notes *****/
		if (trgtWorkNotes.indexOf(workNotes) < 0) {
			// OnAfter business rules cannot process comments so we need to pull from the journal
			trgt.work_notes = workNotes;
			trgt.update();
		}
	}

	/**
	* Description: Cleans up the journal entry to remove timestamp, audit information and trim any white space.
	* Parameters:
	* @PARM1 - journalEntry (String) - Journal entry by index number i.e. current.work_notes.getJournalEntry(1)
	* Returns: note (String) - cleaned up journal entry with just the work_notes information.
	*/
	function cleanWorkNotes(journalEntry){
		var note = "";
		if (journalEntry) {
			//We need to clean-up the journal entry
			//return all text after the first line break in the journal entry (first line contains timestamp and audit information).
			note = journalEntry.substr(journalEntry.indexOf('\n')+1);
			note = note.trim(note); //remove any whitespace before and after string
		}

		return note;
	}

})(current, previous);

Let me know if it worked for you.

Brent

P.S. If my suggestion helped then please mark as helpful and/or correct so other community members can benefit from this information.

View solution in original post

31 REPLIES 31

the encoded query message is coming from the same script. there is an info message after gr.query();

so this piece of code works and it updates the target records worknotes. at this point i am pretty sure the issue is because of usage of 'setWorkflow(false)' but if i dont use it, the code goes in an infinite loop.

 

find_real_file.png

I think you are right. It could be the setWorkflow(false) that's causing the issue. Let's modify your script a little.

1. Find out the sys_id of one of these tasks that you got in your screenshot.

2. Remove the while loop and setWorkflow(false)

3. Use the sys_id obtained in #1 to update work notes with the same code you are using right now.

4. Does the work note get updated?

 

Troy Riblett
Giga Guru

setWorkflow(false) means that worknotes will not be added correctly as they are stored in a different table (sys_journal_field).

One thing to try would be to insert the values directly into that table.

To give that a try you add the following code right below your gr.update();

var journalGR = new GlideRecord("sys_journal_field");
journalGR.name = "sc_task";
journalGR.value = gr.work_notes;
journalGR.element = "work_notes";
journalGR.element_id = gr.GetUniqueValue();
journalGR.insert();

No guarantees that will work though

i tried something like this and tested a request item with 2 catalog tasks. this async business rule is on the journal entries table.

find_real_file.png

and it creates another entry by copying the text '546' for another task (which is great)

find_real_file.png

but i dont see it on the work notes

 

find_real_file.png

 

find_real_file.png 

Hi!

I have tried your suggestion above and it is working. I can see the journal entries in the sys_journal_field table BUT I can't see the journal updates in the activity feed of the target record. I have the Activities filtered to show the journal field. Strange thing is I can see the update in the list view, so I'm a bit stumped - any ideas?

Journal entry appears in list view:

find_real_file.png

 

But not in Activities feed:

find_real_file.png

 

Activities feed is filtered to show 'Comments'

find_real_file.png