Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How do I get a ScriptRunner Listener to fire only once per met criteria?

Karl Samson
Contributor
April 26, 2024

We are trying to write a simple Listener that will fire only the once per a certain field is being changed to a set criteria. The Condition works fine, but we are struggling to set it to the right Event; as the Custom email is sent when the condition is met, but also every time an issue with this criteria is updated.

 

cfValues['Custom Field']?.get(null)?.value == 'Yes', as it is a Cascade cf.
It might be as simple as selecting the right 'Event', but if anybody knows perhaps how to code the 'Condition' appropriately, that would be great!

1 answer

0 votes
Radek Dostál
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.
April 26, 2024

When you use 'cfValues(..)' it will look up the field value each time the listener is triggered, so I assume every time the issue is updated, just guessing that that is the event it listens to.

What you probably are after is verifying that the custom field has actually been changed as part of this event -- i.e., only check if the field has been changed, if so, check the value and do x, if the field is not being changed in this event, do nothing.

Something like this could work:

def changeItem = event.getChangeLog()?.getRelated("ChildChangeItem")?.find {
it.field == "Custom Field Name"
}
if (changeItem != null) {
// field is being changed, we can either read the data from changeItem, or we can re-use the built-in method
if ("Yes".equals(cfValues["Custom Field Name"]?.get(null)?.getValue())) {
// do something
}
}

 

I'm not finding a proper article for issue event handling other than random stuff from search engine such as this: https://stackoverflow.com/questions/36668135/how-to-get-a-list-of-modified-fields-with-scriptrunner-on-issueupdated-event

Though for sure I remember seeing a decent article, I'll update if I find it, but above I suppose should work for a start.

 

Ideally, the listener should read the changeItem to get the new value, rather than check the existing issue, but it won't make a practical difference. I do believe that 

changeItem.newstring == 'Yes'

would work, but again, it's practically irrelevant, at best a few nanos in runtime saved.

Karl Samson
Contributor
April 29, 2024

Hi Radek, sorry I can't get the above script to work? Also, the second piece, 'changeItem.newstring =='Yes'', where would this go in the top script, or what would it replace please? Thanks.

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
9.12.7
TAGS
AUG Leaders

Atlassian Community Events