Script Runner - Setting fields for issue cloned with Cloning Functionality Edited

Hello guys,

I'm using the scripting post function from Script Runner that clones one issue from one project to the other (Clones an issue and links), and i'm trying to set a field on the target clone issue by inserting code on the 'Additional issue actions' box on the post function setup, but maybe i'm doing something wrong, it is not working.

The code is to set a Select Field to a set value:

import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.bc.issue.IssueService.UpdateValidationResult
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.user.ApplicationUser
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.customfields.option.Options
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.*

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

CustomField customField = customFieldManager.getCustomFieldObject("customfield_10802");

OptionsManager optManager = ComponentAccessor.getOptionsManager();
Options options = optManager.getOptions(customField.getRelevantConfig(issue));  

Option newOption = options.getOptionForValue("Three", null);

ModifiedValue mVal = new ModifiedValue(issue.getCustomFieldValue(customField), newOption );

customField.updateValue(null, issue, mVal, new DefaultIssueChangeHolder());

Am I doing something wrong?
Because if I use this code on a post function in the Create Issue transition, it works. But I really needed that working on the Cloning process.

Kind regards

1 answer

Hi Ariel,

As long as your clone issue post function is the first one (or at least before the Update Issue post function) then in your additional code you will need something like

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue

def issue = issue as MutableIssue
def cf = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue).find {it.name == 'SelectListA'}
def value = ComponentAccessor.getOptionsManager().getOptions(cf.getRelevantConfig(issue))?.getRootOptions()?.find {it.value == "BBB"}
issue.setCustomFieldValue(cf, value)

Please let me know if this does the trick

regards, Thanos 

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