Work notes only copied to one task, not all
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi
My business rule isn’t working as expected. What am I missing? When I add work notes to Task 1, they only update Task 2, but Task 3 doesn’t get any of the notes. It seems to copy notes from Task 1 to Task 2, but Task 3 remains unchanged
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hi @CarolMa6,
I would review the trigger conditions...
it's set "Work notes changes" + "State is Closed complete".
I guess it means that you have a record in closed complete state and after you add WN to it, so it's trigger after that. Adjust current or add a new condition line "State changes to CC".
Make the condition as simple as possible for quick debugging - make it to be always trigerable (e.g. WN changes), then add a WN and you will see if something is happening or not:
- If not,
- it's not the condition being wrong, but the code.
- If yes,
- it was the condition what was not ok and needs some tweaks...
No AI was used in the writing of this post. Pure #GlideFather only
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
Hello @CarolMa6
Use your condition properly and use async BR instead of after BR.
(function executeRule(current, previous /*null when async*/) {
// 1. Get the latest entry
var latestEntry = current.work_notes.getJournalEntry(1);
// Safety check: ensure we actually have text
if (!latestEntry) return;
// 2. Clean the "Double Header" (Optional but recommended)
// Splits the entry by new lines and removes the first line (the timestamp/name header)
var cleanEntry = latestEntry;
var entryParts = latestEntry.split('\n');
if (entryParts.length > 1) {
// Rejoin everything after the first line
cleanEntry = entryParts.slice(1).join('\n');
}
// 3. Query Sibling Tasks
var grTask = new GlideRecord("sc_task");
grTask.addQuery("request_item", current.request_item);
grTask.addQuery("sys_id", "!=", current.sys_id); // Use sys_id for safer exclusion than number
grTask.addActiveQuery(); // Usually we only want to update active tasks
grTask.query();
while (grTask.next()) {
// 4. Apply the cleaned note
// We prepend a small tag so people know where this note came from
grTask.work_notes = "[Copied from " + current.number + "] \n" + cleanEntry;
// 5. CRITICAL: Stop Recursion
// This prevents the other tasks from firing their own rules back at us
grTask.setWorkflow(false);
// 6. Optional: Keep the "Updated" timestamp fresh
// setWorkflow(false) stops the 'sys_updated_on' from changing.
// If you want to show it was updated, you sometimes have to force it,
// but usually, silence is preferred for synchronization.
grTask.update();
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago - last edited 3 weeks ago
Hello @CarolMa6
As per last comment that task 2 is created after task 1 is completed and similarly task 3 after task 2 is closed, so a before insert br with condition as if request item.item is your catalog name. and in code it will be as below:
Goal: When Task 4 is created, look at Task 1, Task 2, and Task 3. If they are closed, grab the last work note from each of them and add it to Task 4.
(function executeRule(current, previous /*null when async*/) {
// 1. Find the "First Born" task for this RITM
var grFirstTask = new GlideRecord('sc_task');
grFirstTask.addQuery('request_item', current.request_item);
// 2. Order by Created Ascending (Oldest first)
grFirstTask.orderBy('sys_created_on');
// 3. We only want the very first record found
grFirstTask.setLimit( 1 ); //do remove spacing in brackets
grFirstTask.query();
if (grFirstTask.next()) {
// Safety check: Ensure the 'First Task' isn't the one we are currently creating
// (This happens if this is the very first task ever created for the RITM)
if (grFirstTask.sys_id != current.sys_id) {
// 4. Get the latest note from that first task
var originalInstruction = grFirstTask.work_notes.getJournalEntry( 1 ); //do remove spacing in brackets
if (originalInstruction) {
current.work_notes = "Carried over from Original Task (" + grFirstTask.number + "):\n" + originalInstruction;
}
}
}
})(current, previous);
and below code if your
Goal: Whether it is Task 2, Task 3, or Task 10... always ignore the immediate predecessor and only copy the instructions/notes from the very First Task (Task 1).
(function executeRule(current, previous /*null when async*/) {
// 1. Find the "First Born" task for this RITM
var grFirstTask = new GlideRecord('sc_task');
grFirstTask.addQuery('request_item', current.request_item);
// 2. Order by Created Ascending (Oldest first)
grFirstTask.orderBy('sys_created_on');
// 3. We only want the very first record found
grFirstTask.setLimit( 1 ); //do remove spacing in brackets
grFirstTask.query();
if (grFirstTask.next()) {
// Safety check: Ensure the 'First Task' isn't the one we are currently creating
// (This happens if this is the very first task ever created for the RITM)
if (grFirstTask.sys_id != current.sys_id) {
// 4. Get the latest note from that first task
var originalInstruction = grFirstTask.work_notes.getJournalEntry( 1 ); //do remove spacing in brackets
if (originalInstruction) {
current.work_notes = "Carried over from Original Task (" + grFirstTask.number + "):\n" + originalInstruction;
}
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 weeks ago
@CarolMa6
See the below working of code as per latest comment. "
The code updates 'Task1' and 'Task2', but 'Task3' only receives work notes from 'Task1' and not from 'Task2'"
script:
(function executeRule(current, previous /*null when async*/) {
var consolidatedNotes = [];
// 1. Query for ALL sibling tasks under the same RITM
var grSibling = new GlideRecord('sc_task');
grSibling.addQuery('request_item', current.request_item);
grSibling.addQuery('sys_id', '!=', current.sys_id); // Don't query self
// 2. Only look for closed tasks (3=Closed Complete, 4=Incomplete, 7=Skipped)
grSibling.addQuery('state', 'IN', '3,4,7');
// 3. Order by creation so the notes appear chronologically (Task 1, then Task 2...)
grSibling.orderBy('sys_created_on');
grSibling.query();
while (grSibling.next()) {
// 4. Get ONLY the very last work note added to that task
var lastNote = grSibling.work_notes.getJournalEntry( 1 ); // remove spacing in brackets while using code.
if (lastNote) {
// We format it so we know which task it came from
var formattedNote = "--- From " + grSibling.number + " ---\n" + lastNote;
consolidatedNotes.push(formattedNote);
}
}
// 5. If we found notes, paste them into the new task
if (consolidatedNotes.length > 0) {
current.work_notes = "Summary of previous tasks:\n\n" + consolidatedNotes.join("\n\n");
}
})(current, previous);
So when I tested this BR on below records it worked fine check the sccreenshots:
Task 1:
Now when task 2 is created it copied worknotes from 1 as showed below:
Now when task 3 is created it copied last worknotes from task 1 & task 2 which can be seen below:
Now when task 4 was created it copied task 1,2,3 last worknotes entered which can be seen below:
If my response has helped you, hit the helpful button, and if your concern is solved, do mark my response as correct.
Thanks & Regards
Viraj Hudlikar.