Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,300,848
Community Members
 
Community Events
165
Community Groups

Script Runner Fragments: Custom web item Condition for Custom Field

Hello community

I have added a button to a Jira Ticket using Script Runner for Jira Server. This worked fine. I only want to show it at a certain project, so I set a condition using jiraHelper.project?.key == "TheKey", this also worked fine! But now, I have to extend this condition and only show this button if a certain value in a Select Custom Field is set. Unfortunately, I have not found a way to do this correctly. I've realized it's possible to add an AND using && and maybe check for a certain issueType like jiraHelper.project?.key == "TheKey" && issue.issueType?.name == 'TheIssueType'. But how should the condition be, if I want to check for a Custom Field? I'm a Script Runner beginner by the way. Thanks a lot for help.

Kind regards
Nicole

2 answers

1 accepted

1 vote
Answer accepted

If your "section" is one that is specific to "issue" (and I guess it is based on what you are saying), then yes, you can use the "issue" variable. But that's not available in all fragment contexts.

Also, if you have the issue variable, the jiraHelper.project is not needed.

Your condition could look like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option
if(issue.projectObject.key == 'TheKey'){
def cf = ComponentAccessor.getCustomFieldObjectsByName('The Custom Field Name')
def cfOption = issue.getCustomFieldValue(cf) as Option
if(cfObption.value == "some option value"){
return true
}
}
return false

With any condition script, what matters is the return value. But with groovy, if not specified, the last evaluated statement is automatically returned. That's why a simple "expressionA && expressionB" as a simple condition works.

But nothing forces you to stick to that. So we can either explicitly make a return statement or set some other arbitrary variable and return that at the end:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option

def showMyButton = false

if(issue.projectObject.key == 'TheKey'){
def cf = ComponentAccessor.getCustomFieldObjectsByName('The Custom Field Name')
def cfOption = issue.getCustomFieldValue(cf) as Option
showMyButton = cfObption.value == "some option value"
}

showMyButton

See my answer above...

Looks like I was a little sloppy in the code I shared here.

Here is a cleaner one 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.Issue

issue = issue as Issue
if(issue.projectObject.key == 'TheKey'){
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectsByName('The Custom Field Name')[0]
def cfOption = issue.getCustomFieldValue(cf) as Option
if(cfOption.value == "some option value"){
return true
}
}
return false

 Line 5 could be removed, its only there to make the script editor understand line 8

That condition block is where this code goes.

Like Nicole Mueller likes this

This works!! Thank you so much!

Hi Peter-Dave

Thanks a lot for your answer. The section where the button should go is jira.issue.tools

What it should do is: Run code and display a dialog

I tried to use your code as a condition, but I get some errors:

groovy_fragments_condition_error01.pnggroovy_fragments_condition_error02.png

And this is where I was trying to add the condition (just to make sure):

groovy_fragments_condition.png

 

Do you have any idea, why this is not working?

Kind regards
Nicole

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Apps & Integrations

Marketplace Partner Spotlight: AppLiger

This month the spotlight is on AppLiger. We caught up with Pavel Pavlovsky, CEO and Product Manager, to learn how the company started and what fuels the team's creativity.    Atlassian:...

406 views 0 7
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