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

How to set custom check box to yes through scriptfield code?

Suresh November 21, 2017

Based the value of another field value we need to set checkbox=yes,

I am trying with following script code.


import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.config.FieldConfig

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("Early Access")//checkBox
def fieldConfig = cf.getRelevantConfig(issue)
def eaIssues = getCustomFieldValue("EA Program Issue")
//check esIssues empty or not, and check to "Yes" if is not empty
if (eaIssues) {
issue.setCustomFieldValue(cf, "Yes")//setting checkBox =Yes
return
}
else {
return null
}

I am getting unsrloved error at  

issue.setCustomFieldValue(cf, "Yes")//setting checkBox =Yes 

 

2 answers

Suggest an answer

Log in or Sign up to answer
1 vote
Joshua Yamdogo @ Adaptavist
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 27, 2017

Hi @Suresh,

It looks you are trying to set another custom field within your scripted field. Is that correct? 

You absolutely cannot set values using scripted fields. Our documentation on the website about script fields states this:

 You can use Groovy to display a "calculated" custom field on an issue. You cannot make any modifications within a calculated field… the only thing you can do is pull information from the current issue, or other issues, or outside of the JIRA environment altogether, and display it.

Setting custom field values in a script field will result in numerous errors including inconsistency of values. 

Suresh November 27, 2017

It looks you are trying to set another custom field within your scripted field. Is that correct?  YES

Ok, can we create Checkbox type script field ?

 

Joshua Yamdogo @ Adaptavist
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 28, 2017

Suresh,

I would suggest using a Script Listener or Post-function to set the other custom field. You could set up a script listener that fires every time the issue is updated and it will set the checkbox to "Yes." 

Using a post-function, you could set the checkbox to "Yes" on a specific transition, like when the issue is created. 

Which one do you think will work better for you?

Josh

Suresh November 28, 2017

Script Listener will be better for us,I am trying following code in script listener: selected custom listner and then "update issue event".But still no luck.Any thing I am missing here.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.config.FieldConfig

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("Early Adopter")//checkBox
//def fieldConfig = cf.getRelevantConfig(issue)
def eaIssues = getCustomFieldValue("EA Program Issue")
//check esIssues empty or not, and check to "Yes" if is not empty
if (eaIssues) {
issue.setCustomFieldValue(cf, "Yes")//setting checkBox =Yes
}
Daniel Yelamos [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 29, 2017

Suresh:

Why don't you use behaviours?

Cheers!

Dyelamos

Suresh November 30, 2017

@Daniel Yelamos [Adaptavist] I am trying as follows in behaviours

But still no luck. Am I missing any thing here.

if (formContents["id"] == null) {

def eaIssues = getFieldByName ("EA Roadmap Issues")
def eAdop = getFieldById ("Early Adopter") //checkbox

String cateval = (String) eaIssues.getValue()

if (cateval != null){
eAdop.setFormValue(options.findAll { it.value in ["Yes"] })

}

}

 Capture.PNG

0 votes
Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 21, 2017

Hello,

I guess you need to set it like this

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.config.FieldConfig



def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf = customFieldManager.getCustomFieldObjectByName("Early Access")//checkBox
def config = cf.getRelevantConfig(issue)
def options = ComponentAccessor.getOptionsManager().getOptions(config)
def optionsToSelect = options.findAll { it.value in ["Yes"] }
issue.setCustomFieldValue(cf, optionsToSelect)
Suresh November 22, 2017

Thank you Matveev

We are getting error at same line and saying can not find matching method.

issue.setCustomFieldValue(cf, optionsToSelect)

 

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 22, 2017

What is your Jira version?

Daniel Yelamos [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 22, 2017

Suresh

Is this error a static field checking? Or is it an actual exception?

Cheers!

DYelamos

Suresh November 22, 2017

Static check type

 

static.png

Suresh November 22, 2017

@Alexey Matveev it's jira 7.2.9

Alexey Matveev
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 22, 2017

It is strange. Try like this

issue.setCustomFieldValue(cf, (Object) optionsToSelect)

What is your Scriptrunner version?

Daniel Yelamos [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 22, 2017

Static field checking doesn't actually mean that it isn't going to work. I agree with Alex that this is bizarre, however I  would recommend that you save the script and check that it works.

Suresh November 22, 2017

Updated the script field code with static error and tried but still no luck, initially i just want check the checkbox =yes

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager

def optionsManager = ComponentAccessor.getComponent(OptionsManager)
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def cf = customFieldManager.getCustomFieldObjectByName("Early Access") // Checkboxes is the NAME of my custom field
def fieldConfig = cf.getRelevantConfig(issue)
def option = optionsManager.getOptions(fieldConfig).getOptionForValue("Yes", null)
issue.setCustomFieldValue(cf, [option])
Suresh November 22, 2017

@Daniel Yelamos [Adaptavist] Can we have any alternate approach to set CheckBox =Yes, based on another custom field value is not empty.

Can we set the above code in scriptfield?

Daniel Yelamos [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 24, 2017

Suresh, sorry for the late response, we've been a bit busy in Adaptavist this week.

I've looked through our code and I can't find a particular way of setting the checkbox.

Let's do this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.customfields.manager.OptionsManager

def optionsManager = ComponentAccessor.getComponent(OptionsManager)
def customFieldManager = ComponentAccessor.getCustomFieldManager()

def cf = customFieldManager.getCustomFieldObjectByName("Early Access") // Checkboxes is the NAME of my custom field
def fieldConfig = cf.getRelevantConfig(issue)
def option = optionsManager.getOptions(fieldConfig).getRootOptions()
log.debug("THE OPTIONS FOR THE CHECKBOX ARE:" + option)

This way, we can see what the options for the configuration of the field is, and set the appropriate one after checking the logs.

Let me know if I can help you further.

Cheers!

Dyelamos

Suresh November 24, 2017

Dyelamos,

I am running this code in script field, may i know the log location.Setting Early Access through EA Pass script field.

 

setting earaly acess though EA-Pass scriptfiled.pngRegards,

Suresh

Daniel Yelamos [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
November 24, 2017

Suresh:

We log to the atlassian main log.

Regards.

Dyelamos

Suresh November 26, 2017

We didn't find in the logs,

do we need add above code in the workflow post function?

Can we achieve this through script field?

TAGS
AUG Leaders

Atlassian Community Events