Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,551,670
Community Members
 
Community Events
184
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
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
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
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
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.

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

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
AUG Leaders

Atlassian Community Events