Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Updating Customfield type Checkboxes through Scriptrunner

Will C
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.
August 23, 2018

Hi there,

 

I am trying to update a custom field called Urgent based on another calculation, my script works fine until I try to update the custom field on the issue. It just doesn't seem to set this correctly.

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import java.util.Date.*
import com.atlassian.jira.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.issue.fields.CustomField;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.sql.Timestamp
import java.util.concurrent.TimeUnit
import com.atlassian.jira.issue.customfields.manager.OptionsManager

def optionsManager = ComponentAccessor.getComponent(OptionsManager)
IssueManager issueMgr = ComponentAccessor.getIssueManager();
CustomFieldManager fieldmgr = ComponentAccessor.getCustomFieldManager();
def issue = issueMgr.getIssueObject("IssueKey")

def customFieldManager = ComponentAccessor.getCustomFieldManager();
def dateFieldObject= customFieldManager.getCustomFieldObject('customfield_11202');

def datenow = new Date()
def issduedate = issue.getDueDate()
def calculation = (issduedate - datenow)
def cfurgent = customFieldManager.getCustomFieldObject("customfield_13700")//checkBox
def fieldConfig = cfurgent.getRelevantConfig(issue)
def option = optionsManager.getOptions(fieldConfig).getOptionForValue("Urgent", null)

if (calculation < 90)
{issue.setCustomFieldValue(cfurgent, option)}
else
{return 'no'}
issue.store()  

I am running this from the console hence calling  getissueobject, if I comment out after the if it gives me "urgent" so it is finding this option, urgent is the only option on this field.

but this line doesn't seem to set it 

{issue.setCustomFieldValue(cfurgent, option)}

ultimately this will go on a workflow transition but I would like to get it working in the console first, any ideas what I'm doing wrong, I have read lots of posts before about this but can't get it working. 

4 answers

1 accepted

6 votes
Answer accepted
Jenna Davis
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.
August 27, 2018

Hello!

Checkbox fields have to be handled a bit differently. Here's an example script of how to set the values in a checkbox field:

import com.atlassian.jira.component.ComponentAccessor 
def customFieldManager = ComponentAccessor.customFieldManager
def optionsManager = ComponentAccessor.optionsManager
def issueManager = ComponentAccessor.issueManager 
def issue = issueManager.getIssueObject("issue_key") 
def customField = customFieldManager.getCustomFieldObjectsByName("checkbox_name")[0]
def fieldConfig = customField.getRelevantConfig(issue) 
def options = optionsManager.getOptions(fieldConfig)
def optionsToSet = options.findAll { it.value in ["option_name1", "option_name2", "etc"] } 
issue.setCustomFieldValue(customField, optionsToSet)

Hopefully this helps you out, please let me know if you have any other questions. :)

Jenna

Will C
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.
August 28, 2018

Hi Jenna,

thank you, it worked! I appreciate you spending your time writing this out for me :)

Kind regards,

Jenna Davis
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.
August 28, 2018

I'm glad it's working for you! :)

If you're happy with my response to this I would greatly appreciate if you could accept it the answer on this question. This let's other people know this question has a correct response so that they can hunt down answers to their questions faster. 

Thanks!
Jenna

Renee Olson July 28, 2020

@Jenna Davis Is there a way to do this using JMWE?  I'm trying to use the configure field option as a post function on a workflow when an issue is transitioned into a specific status.

David Fischer
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 28, 2020
Like Renee Olson likes this
1 vote
Daniel Alonso
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.
May 8, 2019

Hi @Jenna Davis , 

Have you ever done this for Cloud?

Daniel Alonso
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.
May 23, 2019

I found how to do it:

issueInput.fields.customfield_10097 = [[value: '1'],[value:'2'],[value:'3'],[value:'4']]
0 votes
Nicolas Yuste Tirados
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
August 9, 2019

This only works for a Post Function. How would you do it within a Listener?

0 votes
sbhutada
Contributor
August 1, 2019

@Jenna Davis This solution is not working anymore, can you check?

Suggest an answer

Log in or Sign up to answer