Business Rule -Replication of work annotations between two specific tasks.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2023 12:00 PM
Hey, folks!
I'm trying to make every comment in the Task 1 Work Annotation replicated in Task 2, and vice versa, like a kind of chat. However, the following behavior is happening. Can you support me?
This is the code, I don't know what else to do.
(function executeRule(current, previous /*null when async*/) {
// Check if the current task is an "Inventory Update" task
var inventoryTask = new GlideRecord('sc_task');
inventoryTask.addQuery('request_item', current.request_item);
inventoryTask.addQuery('short_description', 'Inventory Update');
inventoryTask.query();
// Check if the current task is a "Regional Confirmation" task
var confirmationTask = new GlideRecord('sc_task');
confirmationTask.addQuery('request_item', current.request_item);
confirmationTask.addQuery('short_description', 'Regional Confirmation');
confirmationTask.query();
// Get the last comment added to the work_notes of the current task
var lastWorkNote = current.work_notes.getJournalEntry(1);
if (inventoryTask.next() && current.short_description != 'Inventory Update') {
if (current.work_notes.changes()) {
// Update the work_notes of the "Inventory Update" task
inventoryTask.work_notes = lastWorkNote.replace(/^(.+?\n)/, '');
inventoryTask.update();
}
}
if (confirmationTask.next() && current.short_description != 'Regional Confirmation') {
if (current.work_notes.changes()) {
// Update the work_notes of the "Regional Confirmation" task
confirmationTask.work_notes = lastWorkNote.replace(/^(.+?\n)/, '');
confirmationTask.update();
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2023 10:06 PM
Please update your code
(function executeRule(current, previous /*null when async*/) {
// Check if the current task is an "Inventory Update" task
var inventoryTask = new GlideRecord('sc_task');
inventoryTask.addQuery('request_item', current.request_item);
inventoryTask.addQuery('short_description', 'Inventory Update');
inventoryTask.query();
// Check if the current task is a "Regional Confirmation" task
var confirmationTask = new GlideRecord('sc_task');
confirmationTask.addQuery('request_item', current.request_item);
confirmationTask.addQuery('short_description', 'Regional Confirmation');
confirmationTask.query();
// Get the last comment added to the work_notes of the current task
var lastWorkNote = current.work_notes.getJournalEntry(1).match(/\n.*/gm).join('').replace(/^\s*\n/gm, "");
if (current.work_notes.changes()) {
if (inventoryTask.next() && current.short_description != 'Inventory Update') {
// Update the work_notes of the "Inventory Update" task
inventoryTask.work_notes = lastWorkNote;
inventoryTask.update();
}
if (confirmationTask.next() && current.short_description != 'Regional Confirmation') {
// Update the work_notes of the "Regional Confirmation" task
confirmationTask.work_notes = lastWorkNote;
confirmationTask.update();
}
}
})(current, previous);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2023 05:35 AM
Now it's duplicated in both tasks. What I need to happen is for the Work Annotation posted in Task 1 to be replicated in Task 2, or what is posted in Task 2 to be replicated in Task 1, but currently there's a duplicated replication in both tasks.
Task 1
Task 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-10-2023 11:43 AM
(function executeRule(current, previous /*null when async*/) {
// Check if the current task is an "Inventory Update" task
var inventoryTask = new GlideRecord('sc_task');
inventoryTask.addQuery('request_item', current.request_item);
inventoryTask.addQuery('short_description', 'Inventory Update');
inventoryTask.query();
// Check if the current task is a "Regional Confirmation" task
var confirmationTask = new GlideRecord('sc_task');
confirmationTask.addQuery('request_item', current.request_item);
confirmationTask.addQuery('short_description', 'Regional Confirmation');
confirmationTask.query();
// Get the last comment added to the work_notes of the current task
var lastWorkNote = current.work_notes.getJournalEntry(1).match(/\n.*/gm).join('').replace(/^\s*\n/gm, "");
if (current.work_notes.changes()) {
if (inventoryTask.next() && current.short_description != 'Inventory Update') {
var lastWorkNoteTask = inventoryTask.work_notes.getJournalEntry(1).match(/\n.*/gm).join('').replace(/^\s*\n/gm, "");
// Update the work_notes of the "Inventory Update" task
if(lastWorkNoteTask != lastWorkNote) {
inventoryTask.work_notes = lastWorkNote;
inventoryTask.update();
}
}
if (confirmationTask.next() && current.short_description != 'Regional Confirmation') {
// Update the work_notes of the "Regional Confirmation" task
var lastWorkNoteTask2 = confirmationTask.work_notes.getJournalEntry(1).match(/\n.*/gm).join('').replace(/^\s*\n/gm, "");
if(lastWorkNoteTask2 != lastWorkNoteTask) {
confirmationTask.work_notes = lastWorkNoteTask;
confirmationTask.update();
}
}
})(current, previous);