Copying custom fields from parent to subtask issues using scriptrunner

Keylane ICT August 5, 2020

I've written a post function via scriptrunner to create subtasks. I've managed to copy over standard jira fields like priority, summary etc.. to the subtask but am finding it difficult to copy the custom field over. This is my code below:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.config.SubTaskManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueFactory
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue


import org.apache.log4j.Logger
import org.apache.log4j.Level
log.setLevel(Level.DEBUG)

Issue parentIssue = issue.getParentObject()
IssueFactory issueFactory = ComponentAccessor.getIssueFactory()

// user admin is allowed to create a new ticket
def jiraAuthenticationContext = ComponentAccessor.jiraAuthenticationContext
def user = jiraAuthenticationContext.loggedInUser
// Required to create tickets
IssueManager issueManager = ComponentAccessor.getIssueManager()

// FOR REA
// Find the id of the systemtest subtask
String REAsubTaskId = ComponentAccessor.getConstantsManager().getAllIssueTypeObjects().find{it.getName() == "Axon Delivery - Realisation subtask"}.id

// Fill subtask parameters
MutableIssue REAnewSubTask = issueFactory.getIssue()
REAnewSubTask.setSummary("REA - bugfix (ST NOK)")
REAnewSubTask.setDescription("REA - see previous ST-subtask for description")
REAnewSubTask.setParentObject(parentIssue)
REAnewSubTask.setProjectObject(parentIssue.getProjectObject())
REAnewSubTask.setIssueTypeId(REAsubTaskId)
REAnewSubTask.setPriority(parentIssue.getPriority())
REAnewSubTask.setReporter(parentIssue.getReporter())

 

// Add any other fields you want for the newly created sub task
Map REAnewIssueParams = ["issue" : REAnewSubTask] as Map
def REAcreatedSubtask = issueManager.createIssueObject(user, REAnewIssueParams)

// Link created subtask to parent
ComponentAccessor.subTaskManager.createSubTaskIssueLink(parentIssue, REAcreatedSubtask, ComponentAccessor.jiraAuthenticationContext.getLoggedInUser())

 

 

 

Could someone enlighten me please?

 

1 answer

1 accepted

1 vote
Answer accepted
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
August 5, 2020

hi @Keylane ICT I'm using setCustomFieldValue function, so for you it could be:

def customFieldManager = ComponentAccessor.getCustomFieldManager()

def customFieldId = 1111L // you have to put your ID here
def customField = customFieldManager.getCustomFieldObject(customFieldId)
REAnewSubTask
.setCustomFieldValue(customField, parentIssue.getCustomFieldValue(customField))
Keylane ICT August 5, 2020

Works like a charm! Thanks @Martin Bayer _MoroSystems_ s_r_o__ 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
7.13.12
TAGS
AUG Leaders

Atlassian Community Events