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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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
Community showcase
Published in Jira

Admins, notify your Jira instance of system-wide changes with the new admin announcement banner

Hi All! We’re excited to share the launch of an announcement banner that lets Jira site administrators communicate directly to their users across Jira Cloud instance.   📢 Get y...

129 views 6 6
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you