Copy work notes from one catalog task to another task.

Reddy
Kilo Sage

Hello All,

I have a script that copies the work notes from TASK 1 to TASK 2 and to TASK 3. The issue is TASK 1 work notes are duplicated in TASK 3.

find_real_file.png

 

find_real_file.png

 

Script:

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

comments.push(gr.work_notes.getJournalEntry(-1));

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

 

Thanks,

SR

1 ACCEPTED SOLUTION

Michael Jones -
Giga Sage

Ok, I think that I see what is going on. 

The date and time are being appended when you get the value of the comment, on the next iteration you see the previous time stamp in addition to the current one. It's weird but, for example, on the first task you only "see" this: 

my first work note

But the script returns:

2020-11-24 12:15:07 - System Administrator (Work notes) my first work note

You don't see the first time stamp because it is displayed

 

On the second run you only see this: 

2020-11-24 12:15:07 - System Administrator (Work notes)
my first work note

But the actual value is:
2020-11-24 12:16:40 - System Administrator (Work notes)
2020-11-24 12:15:07 - System Administrator (Work notes)
my first work note

So it just keeps growing.

Try something like this; it's a little closer to what you are looking for, I think.
Attempts to filter out the extra time stamps...

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();
}


I hope this helps!

If this was helpful or correct, please be kind and remember to click appropriately!

Michael Jones - Proud member of the CloudPires team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

View solution in original post

6 REPLIES 6

sachin_namjoshi
Kilo Patron
Kilo Patron

 

You will have to use scratchpad variable to store sys_id of tasks created in your workflow.

Then you can use this scratchpad variable so that only previous record comments are copied.

 

https://docs.servicenow.com/bundle/geneva-servicenow-platform/page/administer/using_workflows/concept/c_WorkflowScratchpadVariables.html

 

Regards,

Sachin

Michael Jones -
Giga Sage

It seems like the behavior is what would be expected - you are iterating though all previous tasks, capturing the comments and pushing them to the next task. The first task is pushed to the second task. The first task and the second task are pushed to the third task. 

If you only want notes from the most recently created task (sequentially) you could adjust your query like this: 

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;

comments.push(gr.work_notes.getJournalEntry(-1));

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

I hope this helps!

If this was helpful or correct, please be kind and remember to click appropriately!

Michael Jones - Proud member of the CloudPires team!

I hope this helps!
Michael D. Jones
Proud member of the GlideFast Consulting Team!

Thank you, @Michael Jones - CloudPires!! It worked.

But still, the date and time are duplicated for TASK 1 Work notes.

find_real_file.png

Hi  @Michael Jones - 

 

I also have the same requirement.

 

Is this a standard way of doing in SerbiceNow , Copying work notes on all tasks .

 

Will it make any performance issue, Is this a good practice?