Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How do you change a specific custom field value when cloning with Listeners

Diana
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.
January 17, 2023

With Scriptrunner listeners, I'm using a custom script that triggers on Issue Created. When a new issue has a certain radio button value selected, the script will auto create another new issue, cloning the original issue, but it changes a single select list to a different value (field name Team). But the new cloned issue will not properly re-assign. Everything else clones properly.

import com.atlassian.jira.componentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.user.ApplicationUser


//define objects
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def issueFactory = ComponentAccessor.getIssueFactory()
def optionsManager = ComponentAccessor.getOptionsManager()
def issue = event.issue
def radioField = customFieldManager.getCustomFieldObjectsByName("Radio").getAt(0)
def radioFieldValue = radioField.getValue(issue)
def storyType = ComponentAccessor.issueTypeSchemeManager.getIssueTypesForProject(issue.projectObject).find{it.name=="Story"}
def currentUserObj = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

 

//only on features and if radioField is Yes
if(issue,issueType.name == "Feature"){
log.warn("Checking if radioField is needed on Feature: " + issue.key)

if(radioField.toString() == "Yes") {
log.warn("Creating new story since radioField is: " + radioFieldvalue)

//clone issue to a story
def toClone = issueFactory.cloneIssueWithAllFields(issue)
toClone.setIssueTypeObject(storyType)

//create new issue
def cloned = issueManager.createIssueObject(currentUserObj, toClone) as MutableIssue
def newIssue = issueManager.getIssueObject(cloned.key)
log.warn ("new issue: " + newIssue)

//clone epic link
def epic = customFieldManager.getCustomFieldObjectsByName("Epic Link").getAt(0)
def epicConfig = epic.getRelevantConfig(issue)
def epicOptions = optionsManager.getOptions(epicConfig).getOptionForValue(epic.toString(),null)
toClone.setCustomFieldValue(epic, epicOptions)

//multiple of other custom fields also get cloned with no problem

//re-assigned cloned issue to a different team
def team = customFieldManager.getCustomFieldObjectsByName("Team").getAt(0)
def teamConfig = team.getRelevantConfig(cloned)
def teamValue = cloned.getCustomFieldValue(team)
def teamOption = optionsManager.getOptions(teamConfig).getOptionForValue(teamValue.toString(),null)
def assignTeam = teamOption.toString().equals("Team A")
cloned.setCustomFieldValue(team, assignTeam)

log.warn("Assigned to Team: " + teamValue)
}

}

Because the newly cloned issue has null value for Team, how do I set the cloned issue to a specific value?

 

1 answer

1 accepted

0 votes
Answer accepted
Diana
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.
January 18, 2023

figured it out! i'm putting it here for others that need help with similar issue. 

variable "cloned" may be an issue, but it still needs to be a MutableIssue, which is "toClone" in this example. Even if you put "as MutableIssue" it will not recognize your newly created cloned issue as mutable for some reason. You also need to reference to the parent issue that you're cloning from which is "issue"

solution:

//re-assigned cloned issue to a different team
def team = customFieldManager.getCustomFieldObjects(issue).find{it.name == "Team"}
def teamConfig = team.getRelevantConfig(issue)
def teamValue = issue.getCustomFieldValue(team)
def teamOption = optionsManager.getOptions(teamConfig)?.getRootOptions()?.find {it.value == "Team A"}

toClone.setCustomFieldValue(team, teamOption)

 

Suggest an answer

Log in or Sign up to answer