Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,365,477
Community Members
 
Community Events
168
Community Groups

How to copy fields from parent issue to sub tasks?(script-runner)(Groovy)(Post-Function)

I was able to piece together a script that generates multiple sub tasks when the parent issue is created. That being said, I need to be able to transfer some of the fields from the parent issue over to the sub tasks but have not been able to do so. 

I used the lines below as suggested by other posts with no success. 

//def custom fields
def cf_SepDate = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Separation Date")
def cf_Position = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Position Series & Grade")
def cf_reason = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Reason For Separation")

//custom fields values
def cfv_SepDate = issue.getCustomFieldValue(cf_SepDate) as Date
String cfv_Position = issue.getCustomFieldValue(cf_Position)
Option cfv_reason = (Option) issue.getCustomFieldValue(cf_reason)

//creates the subtask
MutableIssue newSubTask = issueFactory.getIssue()
newSubTask.setSummary(summary)
newSubTask.setParentObject(parentIssue)
newSubTask.setProjectObject(issue.getProjectObject())
newSubTask.setDescription(parentIssue.description)

ApplicationUser userA = ComponentAccessor.getUserManager().getUserByName(assignee[i])
log.warn("Assigned User: " + userA.displayName)
newSubTask.setAssignee(userA)
newSubTask.setReporter(userA)
newSubTask.setIssueTypeId(constantManager.getAllIssueTypeObjects().find{it.getName() == issuetypes[i]}.getId())

Map<String,Object> newIssueParams = ["issue" : newSubTask] as Map<String,Object>
issueManager.createIssueObject(user, newIssueParams)
subTaskManager.createSubTaskIssueLink(parentIssue, newSubTask, user)

/*below are the lines of code that I am having issues with.
newSubTask
.setCustomFieldValue(cf_SepDate, cfv_SepDate)
newSubTask.setCustomFieldValue(cf_Position, cfv_Position)
newSubTask.setCustomFieldValue(cf_reason, cfv_reason)

 

Is there any other way to transfer these fields without the use of add-ons? Jira is not showing any error message when the script runs.

 

Thanks in advance for any help

 

 

1 answer

0 votes
Mike Rathwell Community Leader May 20, 2019

@Sebastian Gomez , I'd need to dig around in my scripts (and will if this doesn't get you going) to find the exact syntax but what is happening is the fields are set but you haven't told the DB about it yet. You'll need to add an update step to update the subtasks which will commit the value and generate an issue updated event.

Mike Rathwell Community Leader May 20, 2019

IissueManager.updateIssue(user, newSubTask, EventDispatchOption.ISSUE_UPDATED, true) think you need something like this:

Ok, I will try it out to see if it works. 

when I tried adding issueManager.updateIssue(user, newSubTask, EventDispatchOption.ISSUE_UPDATED, true) to the end, the script stopped working and it doesn't create any sub-tasks. 

The script above is currently running inside an if loop, letting Jira know that if parent issue type is submitted by a user, it needs to create 24 sub-tasks, and I want several fields from the parent issue to be copied over to all of the 24-sub-tasks at the moment of creation. 

I also tried the line below without success.

UpdateIssueRequest.builder().eventDispatchOption(EventDispatchOption.ISSUE_UPDATED).sendMail(false)
Mike Rathwell Community Leader May 21, 2019

Hi @Sebastian Gomez , Without seeing all of your changed code, I am guessing that you need to put the update statement inside the iterative loop that is writing the values to each issue... 

Thank you for the help @Mike Rathwell 

 also able to copy the fields correctly from the parent issue to the subtasks by making the following change:

 

// Since .getIssue() only creates a blank sub-task 
MutableIssue newSubTask = issueFactory.getIssue()


We decided to use .cloneIssueWithAllFields instead. Now the correct fields are transferring over to the sub tasks when they get created.

MutableIssue newSubTask = issueFactory.cloneIssueWithAllFields(parentIssue)

 

Jira Documentation:

https://docs.atlassian.com/software/jira/docs/api/7.1.9/com/atlassian/jira/issue/IssueFactory.html

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events