Pass work notes from one task to the next HELP

jonsr20
Tera Expert

Hey everyone,

 

I have need for comments from task to follow over to the next task in workflows. I have searched and found a post that said it has a solution but it is not working for me, wondering if anyone has any advice or what I am doing wrong. I created a business rule with 'after' update and conditions are work notes changes and state is closed complete. Below is the script but it is not working for me. Any advice would be really helpful! Here is the script what am I doing wrong?

 

task.work_notes=pullComments();
function pullComments(){
var comments=[];
var gr= new GlideRecord("sc_task");
gr.addQuery("request_item",current.getValue("sys_id"));

//order by sys_created_on and limit to one record
gr.orderByDesc('sys_created_on');
gr.setLimit(1);

gr.query();
while(gr.next()){
task.short_description = gr.short_description;
task.description = gr.description;

var wn = gr.work_notes.getJournalEntry(-1);
var regex= new RegExp('\n');
var a = wn.search(regex);

wn = wn.substring(a+1, wn.length);
comments.push(wn);

}
gs.log("From workflow final return "+comments.join());
return comments.join();
}

 

1 ACCEPTED SOLUTION

More string manipulation technics.  This should leave just the name and work note, though you might have 2 names for every entry given the first Notes Example above.  If that's still the case and you can't find what is adding the 'System' you can try to add a replace for 'System (Work note)s' before the existing one.  As a bonus, this approach will (theoretically) still work on 2100-01-01.

(function onBefore(current, previous) {
	var gr = new GlideRecord('sc_task');
    gr.addQuery('request_item', current.request_item);
    gr.addInactiveQuery();
    gr.orderByDesc('number');
    gr.query();
    if (gr.next()) {
		var wnArr = [];
		var wn = gr.work_notes.getJournalEntry(-1).split('\n');
		for (var i = 0; i < wn.length; i++) {
			wn[i] = wn[i].replace('(Work notes)', '');
			var index = wn[i].indexOf(' - ');
			if (index > 0) {
				index += 3;
			}
			wnArr.push(wn[i].substring(index));
		}
		current.work_notes = wnArr.join('\n');
    }
})(current, previous);

 

View solution in original post

24 REPLIES 24

Now that the trigger is correct we can dig into the script.  Given this process flow you probably don't want any Filter Conditions or a Condition on the Advanced tab.  Let's get it working first, then you can add something later if it's running when it shouldn't for some reason.  Work Notes is a journal field, which can add some complexity, but if you're just trying to take whatever might have been in the previous Catalog Task and put it in the new one, then this should work.

function onBefore(current, previous) {
    gs.info ('BR running')
    var gr = new GlideRecord('sc_task');
    gr.addQuery('request_item', current.request_item);
    gr.addInactiveQuery();
    gr.orderByDesc('number');
    gr.query();
    if (gr.next()) {
        gs.info ('BR Catalog Task found ' + gr.number)
        current.work_notes = gr.work_notes;
    } else {
        gs.info ('BR Catalog Task not found.')
    }
}

If it's not working, check the System Logs (search for messages starting with BR).  Once it is working, don't forget to remove/comment out the logs (and the else block in this case) before promoting to production.

I see in the system logs it says, "BR Catalog Task found SCTASK0209808" and "BR running" but that is it. It is not carrying over notes from task. So, it appears to find the task it is just not performing the desired action. Maybe I input it wrong, I removed all conditions and selected before insert and input what you have above like this. 

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

    gs.info ('BR running');
    var gr = new GlideRecord('sc_task');
    gr.addQuery('request_item', current.request_item);
    gr.addInactiveQuery();
    gr.orderByDesc('number');
    gr.query();
    if (gr.next()) {
        gs.info ('BR Catalog Task found ' + gr.number);
        current.work_notes = gr.work_notes;
    } else {
        gs.info ('BR Catalog Task not found.');
    }

        




})(current, previous);

Now that I've had a chance to test this myself, I see you do need that bit from your original script to get all of the journal entries:

(function onBefore(current, previous) {
    var gr = new GlideRecord('sc_task');
    gr.addQuery('request_item', current.request_item);
    gr.addInactiveQuery();
    gr.orderByDesc('number');
    gr.query();
    if (gr.next()) {
		current.work_notes = gr.work_notes.getJournalEntry(-1);
    }
})(current, previous);

Thank you, Brad you are awesome!!! 

Do you know if it's possible to clean up the text at all and remove this system line I have highlighted in the picture? I thought there was something in that original script to perform that, but I do not see it now.

 

Thanks again,

Jon

Was able to clean it up a bit with, but it removes my name and keeps the system entry. Maybe there is no way around that? 

var gr = new GlideRecord('sc_task');
    gr.addQuery('request_item', current.request_item);
    gr.addInactiveQuery();
    gr.orderByDesc('number');
    gr.query();
    if (gr.next()) {
		
		
		var wn = gr.work_notes.getJournalEntry(-1);
var regex= new RegExp('\n');
var a = wn.search(regex);

wn = wn.substring(a+1, wn.length);
		
		
		
		
		current.work_notes = wn;
    }