How to Prevent Z Task Closure if X and Y Tasks Are Still Open?

beycos
Tera Contributor

Hello Community,

I’m designing a flow in ServiceNow where I have three tasks using do the following in Parallel. (see screenshot)

  1. (x)
  2. (y)
  3. (z)

The z task cannot be closed unless either  x or y tasks are in the " Close Complete" state. If either x or y  tasks are still open , the "Close Task" button for the  (z) task should be disabled.

How can I implement this in ServiceNow? Should I use a UI Action, Business Rule, or flow for this?

Thanks in advance for your help!

Regards,

Beyza 

2 ACCEPTED SOLUTIONS

Runjay Patel
Giga Sage

Hi @beycos ,

 

You can create before business rule on task table and use below script.

    var grTask = new GlideRecord('task'); // Replace 'task' with the appropriate table name
    grTask.addEncodedQuery('parent='+current.parent+'^stateIN-5,1,2^sys_id!='+current.sys_id+''); // Fetch tasks X and Y
	
    grTask.query();

    if (grTask.next()) {
        gs.addErrorMessage('Task Z cannot be closed until either Task X or Task Y is in the "Close Complete" state.');
  
		current.setAbortAction(true);
    }

When to run condition

RunjayPatel_0-1736229975644.png

 

-------------------------------------------------------------------------

If you found my response helpful, please consider selecting "Accept as Solution" and marking it as "Helpful." This not only supports me but also benefits the community.


Regards
Runjay Patel - ServiceNow Solution Architect
YouTube: https://www.youtube.com/@RunjayP
LinkedIn: https://www.linkedin.com/in/runjay

-------------------------------------------------------------------------

 

View solution in original post

@beycos 

are you sure the group names are correct?

BR should be before update

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

11 REPLIES 11

Bert_c1
Kilo Patron

Seems updating the existing UI action is needed. but how the 3 tasks are related is not specified, are x and y child task of z?

beycos
Tera Contributor

Hello Bert_c1, 

X, Y, and Z are all child tasks of the same parent task.

Regards, 

Beyza 

Ankur Bawiskar
Tera Patron
Tera Patron

@beycos 

it should be a UI action condition to hide the close button

OR

you can use before update BR and check what's the status of task X and Y

If any 1 is open then abort the business rule

BR Condition: current.State Changes to Close Complete && current.request_item.cat_item.name == 'Your Item Name'

Script:

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

    // Add your code here
    var gr = new GlideRecord("sc_task");
    gr.addQuery("request_item", current.request_item);
    gr.addActiveQuery();
    gr.addEncodedQuery('short_descriptionINTaskXShortDesc,TaskYShortDesc'); // give here the unique short description of task x and task y
    gr.setLimit(1);
    gr.query();
    if (gr.hasNext()) {
        gs.addErrorMessage('You cannot close this task as either Task x or Task y is open');
        current.setAbortAction(true);
    }

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

Thank you for your response Ankur. Could you please check this script?? this is Before update BR. Thank you 

not working atm.

image (3).png