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

Scriptrunner - get checkbox values

adrianprocter August 23, 2018

Hi,

I'm trying to create a button on a story screen, that when the user clicks will ultimately read a checkbox (which has 3 possible values, 'Test', 'QA' and 'Prod'), and tick the next checkbox automatically (eg if "Test" is already checked off, it will check off "QA" for me).  The next iteration would then be to transition the story from the "Test" column to the "QA" column, but that is for another day.

I am however stuck at being able to read in the checkbox values.  I've tried all the code fragments I've found here, and none of them seemed to work correctly for me.

I've created the button, and linked it to the action "run code and display a flag".  A basic REST Endpoint runs and displayed the flag.  So far, all good.  When I went to add in some code to read the values and then display the values read in as part of the flag (just confirmation to me that everything is working), I get to the point where i try to read in the values, and nothing happens - the flag doesn't get displayed until I comment out the offending line of code.

Here is my code - the custom check box is called "Validation"

import com.onresolve.scriptrunner.runner.rest.common.CustomEndpointDelegate
import groovy.json.JsonOutput
import groovy.transform.BaseScript
import com.atlassian.jira.component.ComponentAccessor
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.core.Response

@BaseScript CustomEndpointDelegate delegate

validate(httpMethod: "GET") { MultivaluedMap queryParams ->

// the details of getting and modifying the current issue are ommitted for brevity
// def issueId = queryParams.getFirst("issueId") as Long // use the issueId to retrieve this issue

def issueManager = ComponentAccessor.getIssueManager()
def issueId = queryParams.getFirst("issueId") as Long
def issue = issueManager.getIssueObject(issueId)
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Validation");
// def selectedValues = customField.getValue(issue)*.value


def flag = [
type : 'success',
title: "Issue validated",
close: 'auto',
body : "This issue has been validated : " //${selectedValues}"
]

Response.ok(JsonOutput.toJson(flag)).build()
}

As soon as I remove the comments from 

// def selectedValues = customField.getValue(issue)*.value

I get an error in the script window 

scriptrunner error.PNG

I'm not sure why I should be getting this error, it was the accepted solution on a question from this board a couple of years ago (https://community.atlassian.com/t5/Jira-Core-questions/How-to-get-checkbox-value-with-scriptrunner-in-post-function/qaq-p/278604) .  I'm running Jira 7.10.1 and Scriptrunner 5.4.12.  Is there any documentation that you can point in my direction so I can understand what I'm doing wrong?

I've tried reproducing the code from http://scriptrunner-docs.connect.adaptavist.com/jiracloud/script-console.html#_extracting_the_values_from_a_checkbox_custom_field

This comes up with a warning triangle saying "Failed type checking and we don't know why, it's our fault not yours @ line 1, column1 ." amongst other errors.

 

Adrian

 

 

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
adrianprocter August 24, 2018

My script fragment was not passing in the Issue ID.  I was able to get my script working using the code fragment:

 

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 flag = [
type : "success",
title: "Validation",
close: "auto",
body : "$value"
]

Response.ok(JsonOutput.toJson(flag)).build()
}
TAGS
AUG Leaders

Atlassian Community Events