Business rue to abort action

rahul0602
Kilo Contributor

HI All

I have been trying to write a BR which aborts the action     when Ptasks are not closed and   problem state changes to resolved. It is working fine till here, but I need to set the state back to previous one.

before   update BR on Problem

var ptask = new GlideRecord('problem_task');

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

ptask.addQuery('state', 'NOT IN', '6,7');   //for Closed Skipped and Closed Complete,

ptask.query();

if (ptask.next()) {

gs.addErrorMessage('Please close   all the Problem tasks before resolving/ closing the Problem');

current.setAbortAction(true);

current.state = previous.state;}

It is not letting the user to set state= resolved, which is expected...but current.state = previous.state, this is not happening

7 REPLIES 7

Chuck Tomasi
Tera Patron

If you abort, the state is unchanged. The current.state = previous.state isn't going to run, nor does it need to since the save was aborted at that point. I'm assuming this is a before rule (and not after)?


yes, it's a before update BR .


Problem here is, when I try to change to state to resolved, it aborts the action but state appears resolved, even if Ido save and stay...it will throw invalid update but the state appears reolved....it is onlt when Ireload the form the state is restored to it's previous state


find_real_file.png


The reason for this is so the form reflects what you tried to do and can make corrections before moving on.



Example: If I pick field A=1, B=true, C=Yes, and the BR checks field D and decides that the values are incorrect and aborts, I don't want to have to remember what A, B, and C were set to. I should be able to change D to whatever works and continue to save.




I recognize in your case that the 'correct values' are on another set of records, but that doesn't dismiss the fact that your intended values are still displayed properly on the form. I would recommend reverting them as it will get messy very quickly when you actually do get a "good save" (one that is not aborted.) You might have scripts that are trying to display old values, which are the current values, and - you see where this is going?


dave_slusher
ServiceNow Employee
ServiceNow Employee

I don't think that is going to happen. You are aborting the database update, so you can't both abort the update and execute it at the same time. Shouldn't the record be left in the state it was? What is the state after your abort if you don't try to change the value?