How to close request item when all it's tasks are closed

dp11
Tera Guru

Hi,

I have a workflow on requested item table that creates a single catalog task. When this task closes, the item closes as expected. But when users

have added new catalog tasks outside of the WF using the New button, the requested item does not close after all it's tasks are closed. Any idea

how this can be achieved? I have put a 'Wait for Condition' that has the following code, but that doesn't seem to be helping:

checkTask();

function checkTask() {

var task_cnt = 0;

var closed_tasks = 0;

var catalog_task = new GlideRecord('sc_task');

catalog_task.addQuery('request_item',current.sys_id);

catalog_task.query();

task_cnt = catalog_task.getRowCount();

      while(catalog_task.next()){

              // We are checking, whether the state of task   is closed, so "3" is the database value of closed complete state,

              // "4" is closed incomplete and "7" is closed skipped    

              if(catalog_task.state == "3" || catalog_task.state == "4" || catalog_task.state == "7") {

                      closed_tasks = closed_tasks + 1;

              }

      }

      if( closed_tasks == task_cnt)

      {

            answer = true;

      }

}

Thanks,

Debashree

1 ACCEPTED SOLUTION

manikorada
ServiceNow Employee
ServiceNow Employee

Debashree,



The issue here is whenever the Workflow created task is closed then there will be a business rule which will run and invoke the Workflow on RITM. You need to have similarly for Manually created Tasks.


So, have a Business Rule on sc_task something like this:


Name: Invoke Workflow On RITM


When : onAfter


Order: 800


Condition : current.wf_activity.nil() && (current.state.changesTo(3) || current.state.changesTo(4) || current.state.changesTo(7))


Script:



// Run any workflow(s) so this task's state changes can be checked


runWorkflow_task();




function runWorkflow_task() {


    if (!current.request_item.nil()) {


          var gr = new GlideRecord('sc_req_item');


          gr.addQuery("sys_id",current.request_item.toString());


          gr.query();


          if (gr.next()) {


          new Workflow().broadcastEventToCurrentsContexts(gr, 'update', null);


          }


    }


}


View solution in original post

28 REPLIES 28

manikorada
ServiceNow Employee
ServiceNow Employee

Debashree,



After you close the task, can you check if they are becoming inactive?


Mani,


Yes, looks like they do become inactive. Snapshot's below:



Capture4.PNG



Thanks,


Debashree


Mani,


I did some more testing and found that the order in which the tasks are closed matters. If the manually created task is


closed first followed by the WF generated one, then the RITM closes as expected. But if the WF task is closed first then


the workflow proceeds to Wait for Condition and hangs there. What can be done so that the order of closing of tasks won't


matter?


Thanks,


Debashree


manikorada
ServiceNow Employee
ServiceNow Employee

Debashree,



The issue here is whenever the Workflow created task is closed then there will be a business rule which will run and invoke the Workflow on RITM. You need to have similarly for Manually created Tasks.


So, have a Business Rule on sc_task something like this:


Name: Invoke Workflow On RITM


When : onAfter


Order: 800


Condition : current.wf_activity.nil() && (current.state.changesTo(3) || current.state.changesTo(4) || current.state.changesTo(7))


Script:



// Run any workflow(s) so this task's state changes can be checked


runWorkflow_task();




function runWorkflow_task() {


    if (!current.request_item.nil()) {


          var gr = new GlideRecord('sc_req_item');


          gr.addQuery("sys_id",current.request_item.toString());


          gr.query();


          if (gr.next()) {


          new Workflow().broadcastEventToCurrentsContexts(gr, 'update', null);


          }


    }


}


Hi Mani,



Thank you so much for the solution. I just now tested it out and it works perfectly!!!


Just need a clarification on something. When I close the manual task, both the business rules get fired ('SNC Run Parent Flow' and 'Invoke Workflow on RITM'). Will this cause any problem in some other situations that I don't foresee now. I tried setting the order of the 'Invoke Workflow on RITM'   workflow to 700 and 1000, but still both WFs got fired always. I was hoping the value of 1000 would prevent the OOB WF from firing.



Thanks,


Debashree