Creat an UI action button that should check all the child tickets are closed before closing the parent

vijay23
Tera Contributor

Hi all,

I have an urgent requirement 

I have to create an UI action button name as 'close' on 'Issue table' that should be visible to only users having some role. when state is resolved in issue table. It should check that all the related "issue tasks"(child table of issue table) should be closed before closing the issues. If all the tasks are closed issues should also be closed. 

I tried the code below but it is not working as expected

Condition - gs.hasRole('issue_mgmt_worker')

code- 

var rec = new GlideRecord('u_issue_task');

rec.addQuery('u_parent_issues', current.sys_id);

rec.addQuery('state', '3');

rec.query();

if(rec.next()){

gs.addErrorMessage('All issue taks should be closed before resolving/closing issue ' + current.number + ' record'); // use valid number field

current.setAbortAction(true);

}
else
{
current.state='3';
current.update();
}
action.setRedirectURL(current);

 

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Vijay,

Did you try adding logs to check till where it is working fine

Check is the number field a valid field?

Should it not be u_number since it is your custom table

var rec = new GlideRecord('u_issue_task');

rec.addQuery('u_parent_issues', current.sys_id);

rec.addQuery('state', '3');

rec.query();

gs.info('Open Issue Tasks' + rec.getRowCount());

if(rec.next()){

gs.info('Inside if');

gs.addErrorMessage('All issue taks should be closed before resolving/closing issue ' + current.number + ' record'); // use valid number field

current.setAbortAction(true);

}
else
{
current.state = '3';
current.update();

action.setRedirectURL(current);
}

Regards
Ankur

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

View solution in original post

8 REPLIES 8

Elijah Aromola
Mega Sage

I think you need to check if the state is not 3, try the below code:

var rec = new GlideRecord("u_issue_task");

rec.addQuery("u_parent_issues", current.sys_id);

rec.addQuery("state", "!=", "3");

rec.query();

if (rec.next()) {
  gs.addErrorMessage(
    "All issue taks should be closed before resolving/closing issue " +
      current.number +
      " record"
  ); // use valid number field

  current.setAbortAction(true);
} else {
  current.state = "3";
  current.update();
}
action.setRedirectURL(current);

1 u_parent_issues should be a reference field

2. rec.addQuery('state', '3'); in this line 2 should be without '' as state is an integer field.


Raghav
MVP 2023

Brad Tilton
ServiceNow Employee
ServiceNow Employee

It sounds like you want to check if there are any open tasks, and if so abort the action and give the user a message. In your code you're checking for closed tasks instead. You might change the state query line to:

rec.addQuery('state', '!=', '3');

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Vijay,

Did you try adding logs to check till where it is working fine

Check is the number field a valid field?

Should it not be u_number since it is your custom table

var rec = new GlideRecord('u_issue_task');

rec.addQuery('u_parent_issues', current.sys_id);

rec.addQuery('state', '3');

rec.query();

gs.info('Open Issue Tasks' + rec.getRowCount());

if(rec.next()){

gs.info('Inside if');

gs.addErrorMessage('All issue taks should be closed before resolving/closing issue ' + current.number + ' record'); // use valid number field

current.setAbortAction(true);

}
else
{
current.state = '3';
current.update();

action.setRedirectURL(current);
}

Regards
Ankur

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