Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,552,846
Community Members
 
Community Events
184
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
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Nov 22, 2021

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...

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Nov 23, 2021

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

Hi @Nicole Mueller , have you tried the following?

 jiraHelper.project?.key in ["project1", "project2"]

and if you want the opposite you and the exclamation at the front, then encase the condition in parenthesis like this:

!(jiraHelper.project?.key in ["project1", "project2"])

 

Hope that helps!

My mistake. Read the question incorrectly!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events