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

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

1 vote
Joshua Yamdogo Community Champion Nov 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. 

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 Community Champion Nov 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

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
}

Suresh:

Why don't you use behaviours?

Cheers!

Dyelamos

@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 vote
Alexey Matveev Community Champion Nov 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)

Thank you Matveev

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

issue.setCustomFieldValue(cf, optionsToSelect)

 

Alexey Matveev Community Champion Nov 22, 2017

What is your Jira version?

Suresh

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

Cheers!

DYelamos

Static check type

 

static.png

Alexey Matveev Community Champion Nov 22, 2017

It is strange. Try like this

issue.setCustomFieldValue(cf, (Object) optionsToSelect)

What is your Scriptrunner version?

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.

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])

@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?

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

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

Suresh:

We log to the atlassian main log.

Regards.

Dyelamos

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?

Suggest an answer

Log in or Join to answer
Community showcase
Teodora [Botron]
Published Feb 15, 2018 in Marketplace Apps

Jira Inferno: The Nine Circles of Jira Administration Hell

If you spend enough time as a Jira admin - whether you are managing a single, mid-sized instance, a large enterprise one or juggling multiple instances at once - you will eventually find yourself in ...

1,197 views 6 19
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot