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

Thanks a million 🙂


You're welcome. Don't forget to mark that last comment Helpful.


muhammad sajjad
Tera Contributor

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

    // Add your code here
    var OpenProbTask = new GlideRecord("problem_task");
    OpenProbTask.get(current.problem_task.number);
    
    if(OpenProbTask.active);
   current.setAbortAction(true);
    gs.addErrorMessage("A attached problem task is not closed. Please close all problems tasks before closing parent problem.");
})(current, previous);

 

 

check before business rule with this script