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

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!

Yayy!! It worked. Thank you.