Can ScriptRunner custom post-function cause transition to fail?

C_ Derek Fields
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 9, 2021

I can't find an answer to this question in the Adaptavist documentation: I want my transition to fail if my custom post-function script fails. Is there a return code that will cause the transition to fail?

3 answers

0 votes
Matthew Stublefield
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 9, 2021

I'm not a groovy developer, so I can only think of an ugly way to do this... but maybe the ugly way would work, and I can't think of an alternative.

Have your transition with the post function actually be circular, so it points back to the same status.

Then, have a fast-track transition that executes after your circular one. The condition would be that the post function had to do whatever it did. If the post function failed, then the fast-track transition wouldn't execute.

The fast-track transition could also be setup with a condition so it's not visible to users and can only be executed through the automation.

Not knowing exactly what you're trying to accomplish with the post function, it's hard to know if the above would work, but this is one way you might be able to accomplish what you're trying to do.

C_ Derek Fields
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 12, 2021

This is an interesting idea and I will have to think about it a little more. Thanks for suggesting it.

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 9, 2021

The only way to "fail" a transition in a post-function is to crash it completely.  It certainly is possible to write code that will always crash, but you really really do not want to be doing this.

Post-functions are not expected to break like this, they are expected to do things as part of a validated and successful transition.

If you crash out a post-function, you can easily corrupt your data, because part of the status change will have happened and other bits will not.  Crashed post-functions are the number-one reason that people end up with issues they can't transition any more because they're in two different states and with corrupted data.

Never write a post-function script to fail.  Write a validator script that checks if your post-function might fail, and fail there instead.

Matthew Stublefield
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 9, 2021

I suspect the problem with the validator approach is that validators check to see if something is there, but the "something" in this case isn't "there" yet because the post function is what's doing the needful.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 9, 2021

Yes, I've seen this a few times, mostly where the post-function needs to update a remote system and that can fail.

In the cases where you can't validate before acting, the usual resort is "flagging" - add some data to the issue that indicates "I tried to do this, but it failed".  Almost always followed up with a service that searches for the flag being set and tries the remote update again, over and over, until it works (and then clears the flag of course).  Similar to your looped transition function.

C_ Derek Fields
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 12, 2021

I will have to think about the validator function. I could test whether the actual step would succeed and notify the user in the transition if not.

@Nic I understand what you are saying about not have scripted post-functions that fail and leave the ticket in a unstable state. However, it would be possible for a decent workflow designer to come up with backout solutions. In my case, the post-function would be first in the list so that if it fails, nothing else would be done.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 12, 2021

The only "back out solution" I can see is even more complex - you'd need to write "this failed" on to the issue somewhere, then write a listener to catch every change and look for the flag after the fact, then work out how to transition back to the previous state and overwrite all the other changes made during the transition back on to the issue based on what the original value was.

This is because, as I didn't emphasise enough earlier, a post-function makes changes based on the fact that a transition has succeeded.  They have no other way to work.  They happen after the decision to proceed has been made.  You can't cancel or revert the "hitting the ground" post-function after you've let go of the weight you are dropping off the tower of Pisa.

0 votes
C_ Derek Fields
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
July 9, 2021

Running a very simple test:

assert 1 == 0

causes the transition to fail with an ugly assertion message. Not what I want.

throw new Exception("Failure message") 

does not cause the transition to fail.

Still looking for a way to cause my transition to fail.

Suggest an answer

Log in or Sign up to answer