- 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:54 AM
Hi,
We only have the one state for closing and that is called 'Close/Resolve'.
When I click the button it sets it to that state. and when I do it manually I set it to that state too..
I wonder is it to do with the order in which the button saves the ticket or something.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2016 08:01 AM
No it should have triggered the business rule, since both are the server side scripts. Can you post your UI action script here
Thanks,
Abhinay
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2016 08:04 AM
No probs, thanks so much,
closeResolvePB();
function closeResolvePB() {
var assgUser = gs.getUserID();
var hg = new GlideRecord ('sys_user');
hg.addQuery ('sys_id', assgUser);
hg.query();
while (hg.next()) {
current.assignment_group = hg.u_home_group;
current.assigned_to = gs.getUserID();
current.problem_state = 4 ;
current.update();
gs.addInfoMessage('Problem ' + current.number + ' has been set to Close/Resolved');
}
var incident = new GlideRecord("incident");
incident.addQuery("problem_id", "=", current.sys_id);
// incident.addQuery("incident_state", "=", 3);
incident.query();
while (incident.next()) {
if (incident.state != 7)
incident.incident_state.setValue(6);
// incident.active.setValue(false);
incident.update();
gs.addMessageInfo ("Incident " + incident.number + ' closed based on closure of problem '+current.number);
action.setRedirectURL(current);
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2016 08:14 AM
Gerard,
In your UI action you are setting problem_state to 4. But in your business rule you are triggering if the state changes to 4. Change your Business rule condition to this,
current.problem_state.changesTo(4) || current.state.changesTo(4)
Thanks,
Abhinay
Please mark Helpful, Like, or Correct depending on the impact of the response
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎06-16-2016 08:20 AM