Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Work notes only copied to one task, not all

CarolMa6
Tera Expert

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

 

CarolMa6_0-1764764921998.png

(function executeRule(current, previous /*null when async*/ ) {

    var gr = new GlideRecord("sc_task");
    gr.addQuery("request_item", current.request_item);
    gr.addQuery("number", "!=", current.number);
    gr.query();
    while (gr.next()) {
        gr.work_notes = current.getJournalEntry(1);
        // to disable businesss rules on this record we can use setwrokflow false
        //gr.setworkflow(false);
        gr.update();
    }

})(current, previous);
2 REPLIES 2

GlideFather
Tera Patron

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...
_____
This reply is 100 % GlideFather and 0 % AI

Viraj Hudlikar
Tera Sage
Tera Sage

Hello @CarolMa6 

Use your condition properly and use async BR instead of after BR.

VirajHudlikar_0-1764767071011.png

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

 

VirajHudlikar_1-1764767224020.pngVirajHudlikar_2-1764767240685.pngVirajHudlikar_3-1764767254196.pngVirajHudlikar_4-1764767268044.png

VirajHudlikar_5-1764767280626.png