Problem should not close unless the problem task is closed

ursnani
Giga Guru

Can you please help me with this.

The problem should not be closed/resolved unless the problem task is closed.

i had written a before business rule(update)

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

      var gr = new GlideRecord('problem_task');

gr.addQuery('active','true');

gr.addQuery('problem',current.sys_id);

gr.query();

if (gr.next()) {

current.setAbortAction(true);

}

})(current, previous);

though i am getting the message as invalid update but the state is changing to Closed/Resolved.

and also i want to display an alert message for the user saying that problem task is not closed.

Can anyone please help me.

Thanks

1 ACCEPTED SOLUTION

amlanpal
Kilo Sage

Hi Naveen,



The Script you have written is perfectly fine, I believe. Please try to reload the form when the 'Invalid Update' comes up. Because the form is not saved and it remains the previous state actually.


Also from Server Side Script you can not use alert message, but you can use AddInfoMessage (for details Scripting Alert, Info, and Error Messages - ServiceNow Wiki). Please use the below script:




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



  var gr = new GlideRecord('problem_task');


  gr.addActiveQuery();


  gr.addQuery('problem',current.sys_id);


  gr.query();


  if (gr.next()) {


  gs.addInfoMessage('Please close the related Problem tasks first.');


  current.setAbortAction(true);


  }


})(current, previous);



I hope this helps.Please mark correct/helpful based on impact


View solution in original post

12 REPLIES 12

Ujjawal Vishnoi
Mega Sage
Mega Sage

Hi Naveen,



You can add   current.state=previous.state before current.setAbortAction(true) inside if condition.



Regards,


Ujjawal


Thanks for the reply but i dont know for some reason it s not setting the state value to previous value.


There may be some other business rule, changing the value of state. Also check, if there is any client script changing the value.


amlanpal
Kilo Sage

Hi Naveen,



The Script you have written is perfectly fine, I believe. Please try to reload the form when the 'Invalid Update' comes up. Because the form is not saved and it remains the previous state actually.


Also from Server Side Script you can not use alert message, but you can use AddInfoMessage (for details Scripting Alert, Info, and Error Messages - ServiceNow Wiki). Please use the below script:




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



  var gr = new GlideRecord('problem_task');


  gr.addActiveQuery();


  gr.addQuery('problem',current.sys_id);


  gr.query();


  if (gr.next()) {


  gs.addInfoMessage('Please close the related Problem tasks first.');


  current.setAbortAction(true);


  }


})(current, previous);



I hope this helps.Please mark correct/helpful based on impact