Can I make a script run every time a custom field value is changed?

Cole
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.
December 7, 2016

I have a custom cascading field called support group and every time this field is changed I want it to change the assignee to a specific user based on the new option. I could use this same idea for many different things. So is it possible to run a script when this value is changed, not on a transition, and not on every save?

1 answer

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

1 vote
Thanos Batagiannis _Adaptavist_
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.
December 7, 2016

Hi Cole,

you can use a listener for an issue updated event and then with in your script you can check which field is changed and do something. 

def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find {it.field == "Name of custom field"}
 
if (change) {
	// do something 
	log.debug "Value before update : ${change.oldstring}"
	log.debug "Value after update : ${change.newstring}"
} else {
	// something changed but is not the custom field with name 'Name of custom field' so do nothing...
}
Cole
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.
December 9, 2016

What is the "ChildChangeItem"?

JamieA
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.
December 12, 2016

It's just an implementation detail - each one represents a single change to an issue. The code Thanos gave you shows how to see if a particular field changed.

Cole
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.
December 12, 2016

So I don't need to change that to match something on my end?

Thanos Batagiannis _Adaptavist_
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.
December 12, 2016

You will need to use your custom field's name to match your's, unless is named 'Name of custom fieldsmile

 

Cole
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.
December 12, 2016

Right. Just wanted to make sure "ChildChangeValue" wasn't something I needed to fill in as well.

 


Going to try something like this with my field values. I will report back after testing.

def change = event?.getChangeLog()?.getRelated("ChildChangeItem").find {it.field == "Name of custom field"}
 
if (change) {
    // do something
 
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def fieldName = "testCascadingSelectList"
def field = getFieldByName(fieldName)
def customField = customFieldManager.getCustomFieldObjectByName(fieldName)
def fieldConfig = customField.getRelevantConfig(getIssueContext())

def options = optionsManager.getOptions(fieldConfig)
def parentOption = options.find {it.value == "A"}
def childOption = parentOption?.childOptions?.find {it.value == "A1"}

field.setFormValue([parentOption.optionId, childOption.optionId])

    log.debug "Value before update : ${change.oldstring}"
    log.debug "Value after update : ${change.newstring}"
} else {
    // something changed but is not the custom field with name 'Name of custom field' so do nothing...
}
Thanos Batagiannis _Adaptavist_
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.
December 12, 2016

hey Cole,

hm I am afraid this is not going to work. The reason is that the script I provided you is for a ScriptRunner Listener but the script you pasted uses a Behaviour

You can do it with both ways. But have a look in behaviours limitations and choose wisely. 

Please let me know if you need further assistance. 

regards, Thanos

Cole
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.
December 12, 2016

So what would you recommend for me? I just combined your script with a cascading select list script. I am not too familiar with Listener vs Behaviour.

JamieA
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.
December 13, 2016

It's easiest to do this in a listener. A listener fires after the user submits the form. It will overwrite whatever value they have selected for the field, if any. It really depends what you want to achieve. Do you want to just set the value, or do you just want to default a value and let the user change it?

Cole
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.
December 14, 2016

I am wanting to change the cascading field options when a certain field is changed to a certain value. Users will be able to change the cascading field after. This essentially moves the ticket from one teams queue to another team.

My logic: If "Procurement Status" = "Received" then set cascading field "Support Group" = ("Desktop","Support") 

TAGS
AUG Leaders

Atlassian Community Events