Script so that Problem cannot be closed with Open Tasks

germc
Giga Expert

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;  

}

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

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);


View solution in original post

27 REPLIES 27

Regarding the popup - I got this handy little trick from serviceNow guru - just use this code and replace the variable string with your message:



var msg = 'Put your message here.';


  gs.addErrorMessage('<script>var t=setTimeout(function(){alert("' + msg + '")},500);</script>' + msg);


Hi guys,


Im back again.


I have tested the business rule solution and it works when we try save the Problem in a Resolved state, it gives the pop up to say cannot close Problem with open Tasks.


However, as we are saving the problem the 'Save' and 'Save & Exit' buttons disappear and the state stays as 'Closed'.


The disappearing of the buttons happens when ticket is resolved but it doesn't make sense in this case as the ticket should stay open.


Now if we come out of the Problem and back in again it refreshed and we see the buttons again.


Is there anything that can be done so that these do not disappear?


Thanks


Check the conditions on the UI actions to ensure they align with your process.



The value in the Condition field must evaluate to true for the button to appear on the form. FOr example,



current.state == '5'



If the current record's state field is 3, the condition will evaluate to false and the button will not appear.



UI Actions - ServiceNow Wiki


Hi, yes, the UI action condition is set to 'current.state!=3 && current.state!=4' which evaluates to show button if these states are not selected.


However, we do want the buttons to disappear when the Problem is closed, we just don't want them to disappear in this instance, if the Problem has open Tasks.


Gerard,



You will have to write a script include to count the child tasks and add that to your condition.



Something like:


current.state!=3 && current.state!=4 && openChildTasks(current) != 0



Then write a script include called openChildTasks with a function like this (UNTESTED):



function openChildTasks(rec) {


        var count = -1;


        var task = new GlideAggregate('problem_task');


        task.addAggregate('COUNT');


        task.addQuery('parent', rec.sys_id);


        task.query();



        if (task.next())


                  count = task.getAggregate('COUNT');



        return count;


}