Getting historical comments to activity stream of another record

MaharshiC
Tera Contributor

Hi,

I have a order and child_task table in my custom app both extended from task table. Now i want whenever I create a new child_task for an order then all the comments(historical comments) that were added for the existing order even before the child task was created should come to the child_task activity stream as well. However I am not able to implement this.

1 ACCEPTED SOLUTION

pundalik
Tera Expert

Hi @MaharshiC ,

 

You can try using Business Rule for this scenario.
1. Create Business Rule on 'child_task' table, that runs on insert operation.
2. Add condition to check it has parent Order record. (eg. For incident it is Parent incident)
Copy All Notes to Child record 01.jpg
3. in Advance section add below script.
Copy All Notes to Child record 02.jpg

SCRIPT:

(function executeRule(current, previous /*null when async*/ ) {
    var grNotes = new GlideRecord('sys_journal_field');
    grNotes.addQuery('element_id', current.<parent_record_field_name>);
    grNotes.orderBy("sys_created_by"); // sort by time from old to latest
    grNotes.query();

    var grInc = new GlideRecord("<table_name>");  // Glide child table
    grInc.get(current.sys_id);
    while (grNotes.next()) {
        if (grNotes.element == "work_notes") // if Note type is worknote
        {
            grInc.work_notes = grNotes.value.toString();
        }
        if (grNotes.element == "comments") // if Note type is comment
        {
            grInc.comments = grNotes.value.toString();
        }
        grInc.update();
    }
})(current, previous);


Follow the above steps and verify the result.
If my solution help you to resolve your query, then please mark it as Helpful and accept the solution.

Thank You!
Pundalik

 

View solution in original post

1 REPLY 1

pundalik
Tera Expert

Hi @MaharshiC ,

 

You can try using Business Rule for this scenario.
1. Create Business Rule on 'child_task' table, that runs on insert operation.
2. Add condition to check it has parent Order record. (eg. For incident it is Parent incident)
Copy All Notes to Child record 01.jpg
3. in Advance section add below script.
Copy All Notes to Child record 02.jpg

SCRIPT:

(function executeRule(current, previous /*null when async*/ ) {
    var grNotes = new GlideRecord('sys_journal_field');
    grNotes.addQuery('element_id', current.<parent_record_field_name>);
    grNotes.orderBy("sys_created_by"); // sort by time from old to latest
    grNotes.query();

    var grInc = new GlideRecord("<table_name>");  // Glide child table
    grInc.get(current.sys_id);
    while (grNotes.next()) {
        if (grNotes.element == "work_notes") // if Note type is worknote
        {
            grInc.work_notes = grNotes.value.toString();
        }
        if (grNotes.element == "comments") // if Note type is comment
        {
            grInc.comments = grNotes.value.toString();
        }
        grInc.update();
    }
})(current, previous);


Follow the above steps and verify the result.
If my solution help you to resolve your query, then please mark it as Helpful and accept the solution.

Thank You!
Pundalik