Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,300,859
Community Members
 
Community Events
165
Community Groups

Adjust Field Behaviour Based on Field Numeric Value

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 Nov 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 ?

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 Nov 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.

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

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

Marketplace Partner Spotlight: AppLiger

This month the spotlight is on AppLiger. We caught up with Pavel Pavlovsky, CEO and Product Manager, to learn how the company started and what fuels the team's creativity.    Atlassian:...

406 views 0 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you