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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:
And this is where I was trying to add the condition (just to make sure):
Do you have any idea, why this is not working?
Kind regards
Nicole
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.