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
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

139 views 1 17
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