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

Copy checkbox values as strings to custom text field?

James Fishwick Dec 05, 2016

Working towards solving this:

Here is some starter code where I try to get the checked values in a custom field and apply them to a text field (on the same ticket). But no joy yet. Some superfluous junk in here, I know.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.project.Project
import groovy.transform.Field
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.config.SubTaskManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.*
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
def cfManager = ComponentAccessor.getCustomFieldManager()
def componentManager = ComponentManager.instance
def optionsManager = ComponentManager.getComponentInstanceOfType(OptionsManager.class)
def customFieldManager = ComponentAccessor.getCustomFieldManager()
//Get parent issue
Issue issue = (Issue) event.issue;
//Check, that it is a subtask type issue
MutableIssue parentIssue = issue.getParentObject();
Set issueLabels = parentIssue.getLabels();
def customField = customFieldManager.getCustomFieldObject("customfield_27132")
def selectedValues = customField.getValue(issue)*.value
def target = customFieldManager.getCustomFieldObject("customfield_27736")
String labels = selectedValues.join(',')
//CustomField cf = cfManager.getCustomFieldObject(condField)
//Options options = cf.getOptions(null, cf.getRelevantConfig(issue), null);
//def cfTarget = cfManager.getCustomFieldObject(target)
//issue.setCustomFieldValue(target, selectedValues.join(","))
//issue.setCustomFieldValue(target, "test")
target.updateValue(null, issue, new ModifiedValue(target, labels), new DefaultIssueChangeHolder());



1 answer

1 accepted

2 votes
Answer accepted
Thanos Batagiannis [Adaptavist] Community Leader Dec 28, 2016

Hi James,

I read your linked question so I suppose what you want to achieve it to set the selected values of a subtask's checkbox field as labels to the parent, implemented as a script runner listener.  

So you will need for the scripting part, something like 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.issue.label.LabelManager

Issue issue = issue

if (! issue.isSubTask())
    return null

def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Checkboxes")
def cfValue = issue.getCustomFieldValue(cf) as ArrayList <LazyLoadedOption>

def valuesToSet = cfValue?.collect {it.value}?.toSet() ?: [] as Set

def parentIssue = issue.getParentObject()
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

// Set<Label> setLabels(final ApplicationUser remoteUser, final Long issueId, final Set<String> labels, final boolean sendNotification, final boolean causeChangeNotification);
ComponentAccessor.getComponent(LabelManager).setLabels(currentUser,, valuesToSet, false, false)

Please let me know if this does the trick.  

PS. If indeed you want to copy checkboxes' values to a text custom field then please let me know so I can modify the script above 

regards, Thanos

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Next-gen

Introducing subtasks for breaking down work in next-gen projects

Teams break work down in order to help simplify complex tasks. This is often done iteratively, with tasks being broken down into smaller tasks and so on until the work is accurately captured in well-...

9,564 views 34 43
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