How to copy of worknotes from related child incidents to parent case?

mania
Tera Contributor

Hi,

1.I have done with the copy of worknotes from case to related child incidents with the below code.

Business rule:

Insert/After and update:

(function executeRule(current, previous /*null when async*/ ) {
    // Add your code here
    if (current.u_add_worknotes.changes() || current.work_notes.changes()) {
        var identifier = '[Comment Sync]';
         var incidentCount = 0;
            var incGr = new GlideRecord('incident');
            incGr.addQuery('u_parent_case', current.sys_id);
            incGr.query();
            while (incGr.next()) {
                incidentCount++;
                var end = current.time_worked.dateNumericValue();
                var start = previous.time_worked.dateNumericValue();
                var diff = end - start;
                var inTimeWorked = incGr.time_worked.dateNumericValue();
                inTimeWorked = inTimeWorked + diff;
                incGr.time_worked.setDateNumericValue(inTimeWorked);
                var y = current.work_notes.getJournalEntry(1);
                if (y.indexOf(identifier) < 0) {
                    incGr.u_add_worknotes = identifier + '\n' + current.work_notes.getJournalEntry(1);
                    //incGr.setWorkflow(false);
                    incGr.update();
                }
            }
})(current, previous);
2.How to copy of worknotes from related child incidents to parent case?
Can anyone please help on this it will be gratefull.
Thanks!
1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@mania 

have business rule on incident table After insert/update

use this

(function executeRule(current, previous /*null when async*/) {
    // Check if worknotes have changed
    if (current.work_notes.changes()) {
        var identifier = '[Comment Sync]';
        
        // Get the parent case
        var parentCase = new GlideRecord('u_case');
        if (parentCase.get(current.u_parent_case)) {
            var y = current.work_notes.getJournalEntry(1);
            if (y.indexOf(identifier) < 0) {
                // Append worknotes to the parent case
                parentCase.u_add_worknotes = identifier + '\n' + y;
                parentCase.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

View solution in original post

4 REPLIES 4

Prasanth Meda
Tera Contributor

You can created an after update BR when Child Incident got updated with Filter Condition 

Work notes changes and Parent Case is not empty

var parentcase = new GlideRecord("<case table>");

parentcase.addQuery("sys_id", current.u_parent_case);

parentcase.query();

if(parentcase.next())

{

parentcase.work_notes = "Update from Child Incident" + current.getValue("number") + ":" + current.work_notes.getJournalEntry(-1);

parentcase.update();

}

}

You can also use Flow Designer for the same to update

 

 

Ankur Bawiskar
Tera Patron
Tera Patron

@mania 

have business rule on incident table After insert/update

use this

(function executeRule(current, previous /*null when async*/) {
    // Check if worknotes have changed
    if (current.work_notes.changes()) {
        var identifier = '[Comment Sync]';
        
        // Get the parent case
        var parentCase = new GlideRecord('u_case');
        if (parentCase.get(current.u_parent_case)) {
            var y = current.work_notes.getJournalEntry(1);
            if (y.indexOf(identifier) < 0) {
                // Append worknotes to the parent case
                parentCase.u_add_worknotes = identifier + '\n' + y;
                parentCase.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

@Ankur Bawiskar 

It worked.

Thanks!

@mania 

Glad to help.

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