Get a first look at what's coming. The Developer Passport Australia Release Preview kicks off March 12. Dive in! 

Copy work_notes from closed task to new task

Chris Petrone
Tera Expert

Hello.

I have a catalog item that generates 2 tasks. Catalog item is submitted and task 1 generates. When task 1 closes, then task 2 generates.

What I would like to do is when task 1 closes, copy ALL work notes from task 1, and add them to task 2.

I only want this functionality on this catalog item as well.

 

I created an after update business rule on the sc_task table when state changes to Closed Complete.

And the check on the catalog item is in the script, but not getting it to work. Any reccomendations?

 

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

    // Ensure task has a RITM
    if (!current.request_item)
        return;

    // Load RITM
    var ritm = new GlideRecord('sc_req_item');
    if (!ritm.get(current.request_item))
        return;

    //Only for THIS catalog item
    if (ritm.cat_item != 'dbc731321b9fc010a4f4fee58d4bcbdf')
        return;

    // Get ALL work notes from closing task
    var allNotes = current.work_notes.getJournalEntry(-1);
    // ALL entries [1](https://www.servicenow.com/community/developer-forum/basic-understanding-of-getjournalentry-1/m-p/1355790)

    if (!allNotes)
        return;

    // Find next active task under the same RITM
    var nextTask = new GlideRecord('sc_task');
    nextTask.addQuery('request_item', current.request_item);
    nextTask.addQuery('sys_id', '!=', current.sys_id);
    nextTask.addActiveQuery();
    nextTask.orderBy('sys_created_on');
    nextTask.setLimit(1);
    nextTask.query();

    if (nextTask.next()) {
        nextTask.work_notes =
            "Copied from previous task " + current.number + ":\n\n" + allNotes;
        nextTask.update();
    }

})(current, previous);
1 ACCEPTED SOLUTION

Swapna Abburi
Giga Sage

Hi @Chris Petrone 

One reason could be that by the time this business rule is getting executed, the 2nd task may not have created. could be few seconds time delay.

 

You can try to write similar business rule upon creation of second task instead.

View solution in original post

4 REPLIES 4

Swapna Abburi
Giga Sage

Hi @Chris Petrone 

One reason could be that by the time this business rule is getting executed, the 2nd task may not have created. could be few seconds time delay.

 

You can try to write similar business rule upon creation of second task instead.

Thank you for your suggestion. I was able to change the business rule for insert of the second task instead and rewrote the script a bit differently to accommodate for this. Working perfectly now.

AnirudhKumar
Mega Sage

I agree with Swapna. Race-conditions are troublesome!

Alternatively you could rewrite your logic so the BR runs on insert of your second task. This way you can be sure that the first task has fully closed and its work notes is available to copy.

 

Also, where are you creating your second task? Flow Designer, or Workflow or another Business Rule? Wherever it is that's being done, I think the 'copy worknotes' logic should go there instead.

yes thank you for your suggestion. I was able to rewrite this for insert of the second task instead which is working perfectly.