Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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 ?

 

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 votes

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)

 

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

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 Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

Staying organized with Jira: best practices for a better project management

Project managers know this problem: A “mountain of work” lays in front of you, and you don’t know how and where to tackle them. Different to-dos lie ahead, but just one task after the other can be ha...

274 views 2 1
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you