<img height="1" width="1" style="display:none;" alt="" src="https://px.ads.linkedin.com/collect/?pid=299788&amp;fmt=gif">
Skip to content

Untitled-139At Isos, we encounter many situations where either clients or we would like to trigger a custom action or script when a custom field changes. With no out-of-the-box Jira solution, we have had to come up with some solid ways to accomplish this. I would like to share with you one of our methods using a generic Groovy script. If you ever find yourself needing to trigger a script based on when a custom field changes (or even based on what value it is changed from or to) you can skip the research and dive into building out your own solution, using the script below as a springboard.

In our use case, we are using a custom groovy script elsewhere in Jira and only want it to run if the custom field value has changed. If using a post function or other mechanism that isn't tied to an event object, the below script can be used to check if the custom field has changed. We use the name "FieldName" for the custom field we want the condition to be checked against. You should update "FieldName" to the name of your custom field.


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.model.ChangeGroup
import com.atlassian.jira.model.ChangeItem
import com.atlassian.jira.issue.history.ChangeItemBean
def customFieldManager = ComponentAccessor.getCustomFieldManager() def cField = customFieldManager.getCustomFieldObjectsByName("FieldName") def cFieldValue = issue.getCustomFieldValue(cField)
//this gets the newest value of the custom field 'FieldName' def newValue = cFieldValue.toString() def changeHistoryManager = ComponentAccessor.getChangeHistoryManager() def changeItems = changeHistoryManager.getChangeItemsForField(issue,'FieldName')
//if field has no change history, this returns false if (changeItems){ //this gets the previous value of the field def lastValue = changeItems.last().getFromString() return true } else{ return false }

Above, you can see that we can be more granular in our condition by using the previous value and new value of the changed custom field. For example, if you want to ensure the new custom field contains a greater numerical value than the previous, or that the new value is equal to some string, we could add logic there to do so by accessing the newValue and lastValue variables.

Conclusion

We just showed you a method for checking if a custom field has changed, allowing you to trigger automation depending on the condition. This can be invaluable in various circumstances, including if you want to only implement some custom functionality based on the updated field value or if you simply only want to trigger some functionality based on the changing of fields. This is a common need for users of Jira. I hope that when you come across one of these use cases in your organization, you can utilize these helpful solutions to quickly fulfill your needs.

New call-to-action

See More From These Topics