Copy the work notes of child to its direct Parent

Elena Spıru
Mega Guru

Hello,

 

I have added a related list on problem table called Child problems (which shows all children the particular problem has).

Now I need to add a logic that copies the work notes from child to its direct parent and I have the following scenario:

Problem A is the parent of Problem B

Problem B is the parent of Problem C

 

scenario 1: When the user writes a comment in work notes in Problem C, I want the specific work note to be copied only to Problem B's work notes.

scenario 2: When the user writes a comment in work notes in Problem B, I want it to be copied to Problem A

 

I created a business rule that copies the work notes correctly BUT the issue is that for scenario 1, the work notes get copied to Problem's B work notes, then it gets copied to Problem A's work notes - which is not desired.

 

Is there a solution to this?

 

Thank you,

Elena

1 ACCEPTED SOLUTION

@Elena Spıru 
yeah you cannot add that in condition here but you can add it in the script in IF condition.

Sample script for your reference

var text = "ServiceNow makes work better"; //this is whole comment
var substring = "work";  //substring to check copied from 

if (text.includes(substring)) {
  console.log("Substring found!");  //dont copy the worknotes in this case
} else {
  console.log("Substring not found.");  //copy the worknotes in this loop.
}

 

Accept the solution and mark as helpful if it does, to benefit future readers.
Regards,
Sumanth

View solution in original post

18 REPLIES 18

Hi @Elena Spıru ,

 

1st solution

add gs.isInteractive()   in the condition filed of the BR

note this won't work properly if integrations are involved

the work note won't even copies to direct parent (if no integrations involved use this)

 

ChaitanyaILCR_0-1747382119128.png

 

2nd solution 

 

in the 10 line just before the parentProblem.update() use  parentParblem.setWorkflow(false)

 

 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

 

Aditya Kumar6
Tera Contributor

Hello,

 

As i understand, we need to ensure that:

When Problem C work note updates, it only triggers copy to Problem B.

The update to Problem B should not trigger further propagation to Problem A if it was due to a child copy.

 

One of the ideas could be : 

 

In your business rule Prefix the work note with a tag like #from_child# and filter it in upstream rules.

 

 

if (current.work_notes.changes()) {
    var note = current.work_notes.getJournalEntry(1);
    if (note.indexOf('#from_child#') == 0) return; // skip if already copied
    
    var parent = current.parent.getRefRecord();
    parent.work_notes = '#from_child# ' + note;
    parent.update();
}

 

 


This will prevent recursive copying.

 

You could also use a Flag Field for Controlled Propagation:

 

  1. Create a custom field (e.g., u_skip_note_propagation) on the Problem table (type: true/false).

  2. In your business rule that copies work notes:

    • Before updating the parent, set this flag on the parent record (temporarily).

    • In the parent’s business rule, check for this flag and exit early if it is set.

Example in Problem Business Rule Script:

 

 

if (current.u_skip_note_propagation) {
   // Reset the flag and skip propagation
   current.u_skip_note_propagation = false;
   current.update(); // Optional: Only if you want to persist reset
   return;
}

// Only proceed if there's a parent
if (current.parent && current.work_notes.changes()) {
   var parent = current.parent.getRefRecord();
   parent.u_skip_note_propagation = true;
   parent.work_notes = current.work_notes.getJournalEntry(1);
   parent.update();
}

 

 


Both of these are coded methodes and i would prefer the first way becuase in that we will not need to create a custom field but i hope this gave you an idea on how you could approach this challenge.

Please accept this solution if this was helpful to you.

 

Thanks,
Aditya

Hey,

 

This is my current logic inside BR, after update:

ElenaSpru_0-1747380133022.png

where should I integrate the first idea so it will work and prevent recursive copying?

Thanks,

Elena

 

Also, I adjusted my logic with the second solution but this works up to a point. Added the new field on the problem table and this is how it looks with actual data while testing it:
Problem PRB0040006 is the parent of PRB0000011

Problem PRB0000011 is the parent of PRB0040031
So:

Adding a comment in PRB0040031's work notes copies into the PRB0000011:

ElenaSpru_1-1747380935217.png

This got copied correctly, only to its direct parent but the checkbox remains as true

ElenaSpru_2-1747381003827.png

So the issue now is that if an user adds a comment into the PRB0000011's work notes, it won't be copied to its direct parent PRB0040006, but it will change the checkbox to false

ElenaSpru_3-1747381122221.png

The updated BR I have:

ElenaSpru_4-1747381223664.png

 

Any ideas how to solve this issue?

Thank you,
Elena

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@Elena Spıru 

that's going to happen as work notes are copied to B and system considers this as update and triggers BR on Problem B and updates Problem A.

In the after update BR on problem try to add this

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

    // Exit if no parent
    if (!current.parent) {
        return;
    }

    // Get the direct parent
    var parentGR = new GlideRecord('problem');
    if (parentGR.get(current.parent)) {

        // Check if the parent of this parent is the current record (to avoid recursion)
        if (!parentGR.parent.nil() && parentGR.parent == current.sys_id) {
            // This is a circular reference or recursive loop - skip
            return;
        }

        // Copy the work note to the direct parent
        parentGR.work_notes = '[From Child ' + current.number + ']:\n' + current.work_notes;
        parentGR.update();
    }

})(current, previous);

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader