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

Community Alums
Not applicable

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?

 

ThalesSantos_0-1699559929935.png

 

ThalesSantos_2-1699559967005.png

 

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);
3 REPLIES 3

harshav
Tera Guru

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

Community Alums
Not applicable

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

ThalesSantos_1-1699623053428.png

 

Task 2

ThalesSantos_2-1699623086103.png

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