How to set default value for custom field only on Create screen?

I'm trying to use Behaviours plugin to set default value for some custom fields when create issue. 

I created Behaviour and added script

import com.atlassian.jira.component.ComponentAccessor
def tasksFld = getFieldByName("Tasks")
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customField = customFieldManager.getCustomFieldObject(tasksFld.getFieldId())
def defaultValue = """[Tasks|https://]""".replaceAll(/ /, '')

if (getFieldScreen().name == "RM CreateScreen 2.0") {
tasksFld.setFormValue(defaultValue)
}

 but I did not get needed result - seems like condition 

getFieldScreen().name == "RM CreateScreen 2.0"

never works. It does not put value to field when issue created and screen "RM CreateScreen 2.0" is used.
Am I doing something wrong?
Maybe there is another method to restrict behaviours only for create issue operation? Or don’t overwrite the custom field if it already exists?

2 answers

1 accepted

resolved with 

if (getAction()?.id != 1) {
return // not the initial action, so don't set default values
}
1 vote
Alexey Matveev Community Champion Nov 08, 2017

Hello, 

I guess the problem is with getFieldScreen().name == "RM CreateScreen 2.0". Are you sure that your create screen is called "RM CreateScreen 2.0"? You can verify it by logging the name of the screen in the behavour. For example you could add to the beginning of your behavour log.error("getFieldScreen().name") and have a look in atlassian-jira.log what the name of the screen is.

But do not do it. Better use another function which is getActionName(). This function always returns the transition name. In your case it will be "Create Issue". Try to replace your code with this one:

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

tasksFld.setFormValue(defaultValue)

}

It must work. 

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
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

149 views 1 17
Join discussion

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