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

How to Use Script Runner Auto Fill CF a Value Only when the CF Value is Empty

xinan liu Jun 02, 2015

I am using script runner to auto fill the CF a value through this way:

Edit the create transition's validator:

def cf = customFieldManager.getCustomFieldObjects(issue).find {it.name == 'Project Name'}
issue.setCustomFieldValue(cf, issue.projectObject.name)
issue.description != null

Then if we create a issue, the CF "Project Name" will be automaticly set to the Project's name.

But we want to do this.

Add the CF "Project Name" to create screen. If the user fill this CF with a value (such as "Internal") when create issue. Then the CF value will be set to "Internal".

If the user leave this CF blank, the CF value will be set to Project's name.

How to do this?

Thanks!

3 answers

0 votes
Matheus Fernandes Atlassian Team Jun 03, 2015

Hi, Xinan Liu.

You probably want something like:

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue

def cf = ComponentManager.instance.customFieldManager.getCustomFieldObjectByName("Project Name")
def value = issue.getCustomFieldValue(cf);

if(value == null ){
	value = issue.projectObject.name
	issue.setCustomFieldValue(cf, value)
}

Also, you should use this as a Script Post-Function rather than a validator as you are not validating the input, but rather post-processing it. 

Matheus Fernandes Atlassian Team Jun 03, 2015

I haven't actually tested this, but if it doesn't work it should at least show you the logic to complete this. :P

xinan liu Jun 03, 2015

Thanks very much! It works. I know this is a Script Post-Function, but I don't konw how to set this code in Script Post-Function, I can't find the palce to write my own code. See the picture at my answer.

Matheus Fernandes Atlassian Team Jun 04, 2015

When you navigate to the transition in the workflow, rather than selecting "Validators", choose the Post-Functions tab. You should see a "Add post-function" link on the right, which will allow you to choose "Script Post-Function". Then just select "Custom script post-function" and use the code you want. :)

xinan liu Jun 30, 2015

Thanks for your help, but I can not find the "Custom scription post-function", as you can see in my new reply.

0 votes
xinan liu Jun 03, 2015

Validator has the palce to write code in Simple script validator.

image2015-6-4 9:19:3.png

 

Script Post-Function has no place to write the code, is that I have to write the code in a file then put the file in JIRA server? And put the file path in "Script file path"?

image2015-6-4 9:19:16.png

0 votes
xinan liu Jun 30, 2015

Is that my version of script runner does not support "Custom scription post-function" ?

My version of script runner is 2.1.17. Thanks Matheus!

image2015-6-30 18:2:40.png

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Jira

Try Jira Cloud for Outlook: Organize your work without leaving your inbox

Hi Atlassian community, My name is Max and I work on the product integration team at Atlassian. I am pleased to announce the early access program for the Jira Cloud add-in for Outlook. This add-in...

1,274 views 2 13
Read article

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