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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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

Suggest an answer

Log in or Sign up to answer
0 votes
Mike Rathwell
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 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
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 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
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 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

TAGS
AUG Leaders

Atlassian Community Events