Script Runner Fragments: Custom web item Condition for Custom Field

Nicole Mueller November 22, 2021

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.
November 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
Nicole Mueller November 23, 2021

See my answer above...

Like Oumar Diallo likes this
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.
November 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
Nicole Mueller November 24, 2021

This works!! Thank you so much!

0 votes
Nicole Mueller November 23, 2021

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

Richard Jenkins September 6, 2022

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!

Richard Jenkins September 6, 2022

My mistake. Read the question incorrectly!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events