Business rules and work notes

sasharma815
Tera Contributor

Hi all so i have been working on this scenario where i have to post work notes of my parent incident to my child incident decription field for this i have written a br for before update of my child incident and wrote this code within my br  

 

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

var pa = current.parent_incident;  
var arr = [];
var gr = new GlideRecord("sys_journal_field");
gr.addQuery("element_id", pa);
gr.query();
while(gr.next()) {
   
   var val = gr.value;
   gs.addInfoMessage(val);
   arr.push(val);
   current.description=arr.toString().split(',');
//    arr.push(val).toString();
//    gs.addInfoMessage(arr);
 
   
}

})(current, previous);
 
Now info message of val shows all the work notes i am getting but when it's value is populated in description field then the value it shows is "org.mozilla.javascript.NativeArray@2714ebaf" anyone knows about this behaviour , what can i do to fix it ?
 
2 ACCEPTED SOLUTIONS

Ankur Bawiskar
Tera Patron
Tera Patron

@sasharma815 

business rule script will be like this

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

    var pa = current.parent_incident;  
    var arr = [];
    var gr = new GlideRecord("sys_journal_field");
	gr.addQuery("element", "work_notes");
    gr.addQuery("element_id", pa);
    gr.query();
    while(gr.next()) {
        var val = gr.value.toString();
        // Optional: skip empty values, or filter by element = 'work_notes' if needed
        arr.push(val);
    }
  
    // Join with newlines for readability
    current.description = arr.join('\n\n'); 

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

View solution in original post

Bhimashankar H
Mega Sage

Hi @sasharma815 ,

 

Can you mark my answer as correct, helpful if you were able to achieve the requirement. This helps in removing this question from unanswered list and helps users to learn from your thread. Future readers with similar kind of question will easily find out. Thanks in advance!

Regards,
Bhimashankar H

View solution in original post

3 REPLIES 3

Bhimashankar H
Mega Sage

Hi @sasharma815 ,

 

The issue you are facing is related to how you're assigning an array to the current.description field, which expects a string, not an array or complex object. The "org.mozilla.javascript.NativeArray@..." value is just the JavaScript engine's toString() representation of your array object, not the contents.

 

Try below code:

First you need to push all the work notes into array then, then by joining next lines update to description field of incident.

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

    var pa = current.parent_incident;
    var arr = [];

    var gr = new GlideRecord('sys_journal_field');

    // Only fetch work notes from the parent incident
    gr.addQuery('element_id', pa);
    gr.addQuery('element', 'work_notes'); // Better scope: get only work notes
    gr.orderBy('sys_created_on'); // To keep correct order, optional
    gr.query();

    while (gr.next()) {
        arr.push(gr.value.toString());
    }

    // Combine all parent work notes into a single string with line breaks
    current.description = arr.join('\n');

})(current, previous);

 

As 

  • Use gr.orderBy('sys_created_on') to maintain chronological order if needed.

  • Since you're changing current.description on a before update Business Rule, this will save the concatenated work notes into the child incident's description before the record saves.

  • Avoid calling gs.addInfoMessage() in a Business Rule as it shows only in UI and may clutter logs.

 

Thanks,
Bhimashankar H

 

-------------------------------------------------------------------------------------------------
If my response points you in the right directions, please consider marking it as 'Helpful' & 'Correct'. Thanks!

Ankur Bawiskar
Tera Patron
Tera Patron

@sasharma815 

business rule script will be like this

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

    var pa = current.parent_incident;  
    var arr = [];
    var gr = new GlideRecord("sys_journal_field");
	gr.addQuery("element", "work_notes");
    gr.addQuery("element_id", pa);
    gr.query();
    while(gr.next()) {
        var val = gr.value.toString();
        // Optional: skip empty values, or filter by element = 'work_notes' if needed
        arr.push(val);
    }
  
    // Join with newlines for readability
    current.description = arr.join('\n\n'); 

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

Bhimashankar H
Mega Sage

Hi @sasharma815 ,

 

Can you mark my answer as correct, helpful if you were able to achieve the requirement. This helps in removing this question from unanswered list and helps users to learn from your thread. Future readers with similar kind of question will easily find out. Thanks in advance!

Regards,
Bhimashankar H