Dynamically populating text field based on selection

I have been experimenting with the Behaviours plugin in JIRA.

I have been successful with many scripts, but I am not able to figure this one out. I want to be able to insert text into a custom text field, if a certain selection is chosen from a select field.

Here is my code:

 

import com.atlassian.jira.component.ComponentAccessor

import static com.atlassian.jira.issue.IssueFieldConstants.*

    

// Get pointers to the custom field(s) required

def cf = getFieldByName("Custom Select list field A")

def cf1 = getFieldByName("Custom text field B")

    

// Get the Value of Custom Field A

def cfVal = cf.getValue().toString()

 

if (getActionName() != "Create Issue") {

    return // not the initial action, so don't set default values

}

if (cfVal == "Selection from Select List"){

   cf1.setFormValue("Default text I want to have input")

}

 

 

I was attempting to follow https://scriptrunner.adaptavist.com/4.3.0/jira/recipes/behaviours/setting-default-fields.html as closely as possible, but can't seem to get it to work.

 

Any help is greatly appreciated!

-Nick

 

1 answer

1 accepted

Hi Nick,

I have attached a sample script below which shows how you can add some default text to a multi line text field when a certain option is selected in a text field and remove the text once the option in the select list field is changed.

// Get pointers to my fields
def textField = getFieldByName("DemoTextFieldMultiLine")
def selectList = getFieldByName("DemoSelectList")

// Get the Value of my Select Field
def selectListVal = selectList.getValue()

// Specify my default text
String defaultText = "This my default text to add to my multi line text field."

// If the Select List Value matches the option A then set some default Text
if(selectListVal.toString() == "A"){
textField.setFormValue(defaultText)
}else{
// If the select Field value is unselected or a different match then clear the default text
textField.setFormValue("")
}

Also if you want to restrict this behaviour to only apply on the create or edit screens then you can do this by Simply adding the workflow used as a Guided Workflow to your behaviours and add a condition to specify when this behaviour should fire.

I have attached a screenshot below which shows how to restrict this behaviour to the edit screen but this could easily be modified to only allow the behaviour to fire on the create screen.

Screen Shot 2016-10-05 at 12.01.38.png

I hope this helps.

Thanks

Kristian

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Published Apr 19, 2018 in Marketplace Apps

Calling all developers––You're invited to Atlas Camp 2018

 Atlas Camp   is our developer event which will take place in Barcelona, Spain  from the 6th -7th of   September . This is a great opportunity to meet other developers and get n...

366 views 0 6
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you