Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Script Runner - Setting fields for issue cloned with Cloning Functionality

Ariel Rosa May 24, 2017

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

Suggest an answer

Log in or Sign up to answer
1 vote
Thanos Batagiannis _Adaptavist_
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 29, 2017

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 

TAGS
AUG Leaders

Atlassian Community Events