- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-24-2024 11:30 AM
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();
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2024 06:09 PM
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);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2024 12:51 PM
Since there are line breaks we can make an array out of it, then loop through the array to push the lines that don't start with a date to a new array, then set the new work_notes to this value. Good for the next 76 years:
(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++) {
if (wn[i].indexOf('20') != 0) {
wnArr.push(wn[i]);
}
}
current.work_notes = wnArr.join('\n');
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2024 02:58 PM
You're a genius!!! lol is there a way to keep the person's name in it that posted the note but remove the other piece? Keep the part that had my name but remove the system part, it will have value to see who posted the note, sorry I know I am asking too much 😅.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2024 03:56 PM
I was only getting one 'header' line per work note entered. I'm sure we could figure out how to keep the name, but do you have any other active business rules that might be affecting the display of the work notes?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2024 04:01 PM
No, I do not see any other Business rules that affect work notes. I would be really helpful to keep the name so the person in the next task knows who posted the note in the previous task. Thank you again for your time!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2024 06:09 PM
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);