I am having trouble with writing ScriptRunner Behaviours

Deleted user February 5, 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

1 vote
Answer accepted
Jean-Théo [Adaptavist]
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 5, 2020

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 February 5, 2020

Thank you so much JT!

Deleted user February 5, 2020

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

Jean-Théo [Adaptavist]
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 5, 2020

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 February 5, 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 February 7, 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
AUG Leaders

Atlassian Community Events