On a form, I have created a custom checkbox, that has 3 potential values, "Test Validated", "QA Validated" and "Prod Validated".
In ScriptRunner, I have created a Script Fragment to add a button to the operations-top-level menu, that has the option "Run Code and show a flag". It is calling a REST Endpoint using the link
/rest/scriptrunner/latest/custom/validate?issueId=${issue.id}
The idea is that the 3 checkboxes start off unchecked, the first time you click on the script fragment, it identifies that nothing is checked, and should tick the "Test Validated" checkbox for me. The next time, it should tick the "QA Validated", the third time "Prod Validated" and finally, display a message that no more validation is necessary. For a future version, the idea is to also transition the story into the next step of the workflow.
My REST Endpoint is defined below, it identifies correctly the next step, and displays the value in the flag, but I am having trouble setting the checkbox value and getting it stored - my code does not appear to be setting the checkbox. I am stuck on what I need to do to get the code to work, I have been unable to find any code snippets that work for me. I'm using Jira 7.10.1 and the latest version of ScriptRunner.
Adrian
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.sal.api.ApplicationProperties
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.transform.BaseScript
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
import groovy.json.JsonOutput
@BaseScript CustomEndpointDelegate delegate
def applicationProperties = ScriptRunnerImpl.getOsgiService(ApplicationProperties)
def issueManager = ComponentAccessor.getIssueManager()
validate(httpMethod: "GET") { MultivaluedMap queryParams ->
def issueId = queryParams.getFirst("issueId") as Long
if (!issueId)
return
def issue = issueManager.getIssueObject(issueId)
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Validation")
def value = issue.getCustomFieldValue(cf)
def fieldConfig = cf?.getRelevantConfig(issue)
switch (value) {
case "[Test Validated, QA Validated, Production Validated]":
value = "Already validated, needs no further validation."
break
case "[Test Validated, QA Validated]":
def optionToSet = ComponentAccessor.optionsManager.getOptions(fieldConfig).find {it.value == "Production Validated"}
issue.setCustomFieldValue(cf, optionToSet)
value = optionToSet
break
case "[Test Validated]":
def optionToSet = ComponentAccessor.optionsManager.getOptions(fieldConfig).find {it.value == "QA Validated"}
issue.setCustomFieldValue(cf, optionToSet)
value = optionToSet
break
default:
def optionToSet = ComponentAccessor.optionsManager.getOptions(fieldConfig).find {it.value == "Test Validated"}
issue.setCustomFieldValue(cf, optionToSet)
value = optionToSet
break
}
def flag = [
type : "success",
title: "Issue approved",
close: "auto",
body : "$value"
]
Response.ok(JsonOutput.toJson(flag)).build()
}
Hello @adrianprocter
You forgot to store changes with issueManager.updateIssue method, like this
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.sal.api.ApplicationProperties
import com.onresolve.scriptrunner.runner.ScriptRunnerImpl
import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.transform.BaseScript
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response
import groovy.json.JsonOutput
@BaseScript CustomEndpointDelegate delegate
def applicationProperties = ScriptRunnerImpl.getOsgiService(ApplicationProperties)
def issueManager = ComponentAccessor.getIssueManager()
validate(httpMethod: "GET") { MultivaluedMap queryParams ->
def issueId = queryParams.getFirst("issueId") as Long
if (!issueId)
return
def issue = issueManager.getIssueObject(issueId)
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Validation")
def value = issue.getCustomFieldValue(cf)
def fieldConfig = cf?.getRelevantConfig(issue)
def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
switch (value) {
case "[Test Validated, QA Validated, Production Validated]":
value = "Already validated, needs no further validation."
break
case "[Test Validated, QA Validated]":
def optionToSet = ComponentAccessor.optionsManager.getOptions(fieldConfig).find {it.value == "Production Validated"}
issue.setCustomFieldValue(cf, optionToSet)
value = optionToSet
break
case "[Test Validated]":
def optionToSet = ComponentAccessor.optionsManager.getOptions(fieldConfig).find {it.value == "QA Validated"}
issue.setCustomFieldValue(cf, optionToSet)
value = optionToSet
break
default:
def optionToSet = ComponentAccessor.optionsManager.getOptions(fieldConfig).find {it.value == "Test Validated"}
issue.setCustomFieldValue(cf, optionToSet)
value = optionToSet
break
}
issueManager.updateIssue(user, issue, EventDispatchOption.ISSUE_UPDATED, false)
def flag = [
type : "success",
title: "Issue approved",
close: "auto",
body : "$value"
]
Response.ok(JsonOutput.toJson(flag)).build()
}
Thanks Mark,
There was one small change I had to make to get it working, as it was coming up with an error message in the log about converting string values. I just changed the code slightly to and it works.
issue.setCustomFieldValue(cf, [optionToSet])
With your help I got it working :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.