Hello,
I am using Automation for JIRA to run a script when the value of a numeric custom field changes and for this, I am referring to the page https://scriptrunner.adaptavist.com/6.16.0/jira/plugins/automation-for-jira.html#_smart_values and section -
The changed field value is available anywhere smart values are supported using the {{fieldChange}}
substitution. Use {{fieldChange.fromString}}
and {{fieldChange.toString}}
to access display values and {{fieldChange.from}}
and {{fieldChange.to}}
to access raw values (for a select field for example).
{{fieldChange}}
only contains the first changed value. If multiple values are changed (e.g. when setting multiple Fix Versions) then you can iterate over these using the {{#changelog.fixVersion}}{{toString}}{{/changelog.fixVersion}}
expression.
This does not work for my number field and I keep getting this error- "
[c.o.scriptrunner.automation.ExecuteScriptIssueActionV2] Script function failed on Automation for Jira rule: Final script for FNBIPL Pillar - Updated, file: <inline script>, error: java.lang.NumberFormatException: For input string: "1.1" java.lang.NumberFormatException: For input string: "1.1""
I am using the below lines of code-
def originalValue = ruleContext.renderSmartValues("{{fieldChange.from}}")
def currentValue = ruleContext.renderSmartValues("{{fieldChange.to}}")
Can someone advise me on how I can fix this? I even tried {{fieldChange.from}} and
{{fieldChange.to}} but it did not help
I setup a number field and created an Automation rule to happen when it changed. Using this script:
def fieldChange = ruleContext.renderSmartValues("{{fieldChange}}")
log.debug "Field change: ${fieldChange}"
def originalValue = ruleContext.renderSmartValues("{{fieldChange.from}}")
log.debug "Original value: ${originalValue}"
def currentValue = ruleContext.renderSmartValues("{{fieldChange.to}}")
log.debug "Current value: ${currentValue}"
I saw this in my Jira logs when it changed:
DEBUG 2022-08-13 16:59:46,598 [onresolve.scriptrunner.runner.ScriptBindingsManager] Field change: ChangeItemBean{field='Some Num', fieldType='custom', from='null', fromString='1.3', to='null', toString='2000'}
DEBUG 2022-08-13 16:59:46,598 [onresolve.scriptrunner.runner.ScriptBindingsManager] Original value:
DEBUG 2022-08-13 16:59:46,599 [onresolve.scriptrunner.runner.ScriptBindingsManager] Current value:
Based on that, it looks like the change item's from & to fields are null for number fields.
I'm not quite sure how you're getting a NumberFormatException from that code. I can only assume that you're trying to convert the values into numbers somehow. Are you, perhaps, calling a method like toInteger on it? That would throw a NumberFormatException for numbers like 1.1, since 1.1 is a floating point number, not an integer.
If that's what you're trying to do, you could use the toBigDecimal() method. For example:
def originalValue = ruleContext.renderSmartValues("{{fieldChange.fromString}}")
def currentValue = ruleContext.renderSmartValues("{{fieldChange.toString}}")
def sumOfValues = currentValue.toBigDecimal() + originalValue.toBigDecimal()
log.debug "Sum of values: ${sumOfValues}"
Note that I'm using toBigDecimal to mask over some of the inherent silliness of floating point math.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.