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

Next challenges

Recent achievements

  • Global
  • Personal

Recognition

  • Give kudos
  • Received
  • Given

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

I am having trouble with writing ScriptRunner Behaviours

Deleted user Feb 05, 2020

I am trying to set two behaviors, but it is very hard because I do not have any coding background. The idea is that the ticket creation screen would hide/show fields depending on what the user chooses.

So the first behavior should be from a dropdown menu with 3 options (SAP, Jira, Other) and the dropdown menu's name is Affected Software. If the user chooses SAP, a textfield to appear which is called Transaction number. If they choose other, another textfield should appear called Please enter software name and otherwise, these should be hidden and not show any other fields.

Here is the code I tried to write:

import com.onresolve.jira.groovy.user.FormField

FormField dropDown = getFieldByName("Affected Software")
FormField other = getFieldByName("Transaction Number")
FormField other = getFieldByName("Please enter software name")

if (dropdown.getFormValue() == 'SAP') {    other.setHidden(false)    other.setFormValue("SAP chosen")
} if else (dropdown.getFormValue() == "Other")    other.setHidden(false) 
    other.setFormValue("Other chosen")
else { other.setHidden(true)
}

The second behavior is a bit simpler. There is again a dropdown field called Is there a workaround with these options(yes, no, I don't know). If the user chooses yes, a field should show up called Explain the workaround. Otherwise nothing should change.

This is the code I tried to write for that one

import com.onresolve.jira.groovy.user.FormField

FormField dropDown = getFieldByName("Is there a workaround?")
FormField other = getFieldByName("Explain the workaround")

if (dropdown.getFormValue() == 'yes') {    other.setHidden(false)    other.setFormValue("yes chosen")
} else { other.setHidden(true)
}

Could you please let me know what I am doing wrong? Thank you in advance!

2 answers

1 accepted

Hello @[deleted] ,

There is a couple of things that could cause the first one not to work.

  1. Make sure that all your variables have a different name (if you changed the names for privacy, ignore this)
  2. You should change your if else to an else if
  3. Your dropDown does not have a capital letter in the rest of your script
  4. I usually always cast the dropdown value to a String before comparing it to a strink
  5. You are missing some brackets

Here is what the code should look like :

import com.onresolve.jira.groovy.user.FormField

def dropDown = getFieldByName("Affected Software")
def other1 = getFieldByName("Transaction Number")
def other2 = getFieldByName("Please enter software name")

if (dropDown.getFormValue().toString() == 'SAP') {
other1.setHidden(false)
other1.setFormValue("SAP chosen")
}
else if (dropDown.getFormValue().toString() == "Other") {
other2.setHidden(false)
other2.setFormValue("Other chosen")
}
else {
other1.setHidden(true)
}

 Let me know if this works for you or if you need more help.

Best regards,

JT

Deleted user Feb 05, 2020

Thank you so much JT!

Deleted user Feb 05, 2020

However, the code is still not working :/ Any ideas?

Do you have any errors? Or is the behavior just not working?

Did you add the script as an initializer or to the field itself?

Deleted user Feb 05, 2020

It is just not working, I tried first to the server-side script below the condition and then in the initializer.

0 votes
Deleted user Feb 07, 2020

Here is the solution if anyone else needs it 

 

def field = getFieldByName("Standards")
def otherField = getFieldByName("Other")

if(field.getValue() == "MyCoolValue"){
    otherField.setHidden(true).setRequired(false).setFormValue("")
} else{
    otherField.setHidden(false).setRequired(true)
}

Suggest an answer

Log in or Sign up to answer
TAGS

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