Adjust Field Behaviour Based on Field Numeric Value

jonny.smith November 22, 2021

Hi,

I'm hoping somebody can help with this.

I'm pretty new to scriptrunner and trying to adjust Custom Field behaviour based on the numeric value of a field being over a certain amount.

I have done this previously in the attached picture using field dropdown options but, it would be great if there was a similar way I could do this based on numeric Value.Script.JPG

Thanks,

Jonny

2 answers

1 accepted

0 votes
Answer accepted
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 22, 2021

Hi @jonny.smith ,

This is absolutely possible. A number field will return a double, meaning you could use : 

 

def numField = getFieldByName("Number Field")
def numFieldValue = numField.getValue()

if (numFieldValue == 10) { ... }
if (numFieldValue > 10) { ... }

Is this what you are asking for ?

jonny.smith November 22, 2021

Hi @Antoine Berry ,

It looks like it should do what I'm after however, I seem to be getting the following errors on the 'if' lines.

Error.jpgError 2.JPG

Thanks,

Jonny

Like Antoine Berry likes this
Antoine Berry
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 22, 2021

Hi @jonny.smith ,

This error will be corrected at execution time. This is because you have not declared numFiledValue as a Double. But it is not necessary in groovy and the comparison will succeed at execution time. To make sure it not throwing any error you could use this instead : 

Double numFieldValue = numField.getValue()

if (numFieldValue && numFieldValue > 20.00) { ... }

This way the console will declare the value as a double and will check that it has a value (i.e. you have filled a value on the screen and therefore is not null) before comparing to 20.00.

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 22, 2021

I think @Antoine Berry  covered most of it... but I notices you have 2 identical if blocks.

That's not necessary. The curly brace indicates a code block that can have multiple lines such as:

def numField = getFieldByName("Total Amount")
def numFieldValue = numField.getValue()
def field1 = getFieldByName("Attachment")

if (numFieldValue > 20.00) {
field1.setHidden(false)
field1.setRequired(true)
}

Or in this case, you can even string those two:

 field1.setHidden(false).setRequired(true)

But I noticed that you don't have a block of code to hide again if the number is smaller.

This would combine all of the above:

def numField = getFieldByName("Total Amount")
def numFieldValue = numField.getValue()
def field1 = getFieldByName("Attachment")

def isBigAmount = numFieldValue > 20.00

field1.setHidden(!isBigAmount).setRequired(isBigAmount)

Like Antoine Berry likes this
jonny.smith November 23, 2021

Hi @Antoine Berry @Peter-Dave Sheehan ,

I have tried out both of these this morning and they both work as required! I've opted to just use the setRequired functionality so the team still have the ability to upload an attachment for under 20.00 if they want to.

Really appreciate the help!

Thanks,

Jonny

Like Antoine Berry likes this
0 votes
jonny.smith November 22, 2021

.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events