- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2016 06:45 AM
Hi guys,
Im trying to write a Client Script so that if a Problem has any open Tasks, a pop up appears saying 'You cannot close a Problem with open Tasks', please close out the corresponding tasks first'.
Below is the onSubmit Client Script I wrote on the Problem table but I cant get it to work.
Has anybody achieved this?
function onSubmit() {
//Type appropriate comment here, and begin script below
var id = g_form.getValue('number');
var close = g_form.getValue('state');
//If state changes to Closed, check if there is a related article
if (close == '4') {
var pt = new GlideRecord("problem_task");
pt.addQuery('source', id);
pt.addQuery('state', '1');
pt.query();
if (pt.next()) {
alert("test");
}
}
return false;
}
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2016 06:51 AM
Hi Gerard,
How about a BEFORE business rule. Much simpler and faster.
Condition:
State | changes To | Closed
Script:
// This code is untested
(function executeRule(current, previous /*null when async*/) {
var pt = new GlideAggregate('problem_task');
pt.addAggregate('COUNT');
pt.addQuery('problem', current.getValue('sys_id'));
pt.addQuery('active', true);
pt.query();
if (pt.next()) {
count = pt.getAggregate('COUNT');
if (count > 0) {
gs.addErrorMessage('You cannot close this problem. It has active tasks.');
current.setAbortAction(true);
}
}
})(current, previous);

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2016 07:26 AM
Thanks for catching that Abhinay. I'll make the update in the original script.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2016 07:39 AM
Thanks guys,
I made the changes and it works when I change the state to Closed and then hit save.
However, we have a 'Resolved Problem' button that closes out the Problem when clicked and it doesn't work with that at all.
Also, is there a way to have the message appear as a pop up. It currently appears like this:

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2016 07:41 AM
Hi Gerard,
What do you mean when you say the business rule doesn't work at all with the Resolve Problem button?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2016 07:46 AM
sorry, yes, when I click the Resolve Problem button it resolves the Problem successfully without and doesn't seem to invoke the business rule.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2016 07:49 AM
Hi Gerard,
To do that, add the resolved value to your condition. Let's say it's "6". You can use the condition
current.state.changesTo(6) || current.state.changesTo(4)
That triggers the script to run when the state is resolved or closed.