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 Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Wednesday in Jira

Join our webinar: How 1B+ feature flag events helped us build the new Jira

Every time you release software, there's a bit of risk – that there's a bug, that something breaks, or that the feature doesn't resonate with customers. Feature flagging helps make high stakes s...

101 views 0 1
Join discussion

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