Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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

Issue Created Listener: Copy Custom Field Value from Parent to Sub-task on Creation Edited

I am trying to write a scripted listener that will copy down a Custom Field's value (Select List) from parent to child on creation.

I am trying to avoid using a post function, as I want this rule to fire across an entire project whenever a sub-task is created. 

I currently have a listener that is working whenever the Parent is UPDATED...

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.MutableIssue

def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == "Source of Request - JudahTest"}
if (change) {
log.debug "Value changed from ${change.oldstring} to ${change.newstring}"

def subtasks = event.issue.getSubTaskObjects()
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Source of Request - JudahTest")

if (subtasks){
subtasks.each {it ->
def options = ComponentAccessor.getOptionsManager().getOptions(customField.getRelevantConfig(it))
((MutableIssue) it).setCustomFieldValue(customField, options.find {it.value == change.newstring})
ComponentAccessor.getIssueManager().updateIssue(event.user, ((MutableIssue) it), EventDispatchOption.DO_NOT_DISPATCH, false)
}
}
}

Unfortunately, simply changing the listening event did not work. I need this to update on Issue Created! Any one out there want to help tackle this?? 

1 answer

Hi,

Judging by your code, it's not working because:

1) There's no changelog in Issue Created event, and thus there's not point in trying to get field values from there. Get them from the issue directly.

2) It's supposed to trigger on sub-task creation, right? If so you need to change the logic of using your issue variable, because right now it is as if you are creating a parent issue, not a sub-task.

With that in mind this should help:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption


def issue = event.issue

if(issue.isSubTask()){
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def parent = issue.getParentObject()
def customField = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Source of Request - JudahTest")
def cfValue = parent.getCustomFieldValue(customField)
issue.setCustomFieldValue(customField, cfValue)
ComponentAccessor.getIssueManager().updateIssue(currentUser, issue, EventDispatchOption.ISSUE_UPDATED, false)
}


Thank you for the suggestion. When we tried it out we get errors on these lines 

issue.setCustomFieldValue(customField, cfValue)
ComponentAccessor.getIssueManager().updateIssue(currentUser, issue, EventDispatchOption.ISSUE_UPDATED, false)

Can not find matching method for both.

Is that because we need to define options to use setCustomFieldValue for a select list (single) field?

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

Staying organized with Jira: best practices for a better project management

Project managers know this problem: A “mountain of work” lays in front of you, and you don’t know how and where to tackle them. Different to-dos lie ahead, but just one task after the other can be ha...

224 views 2 1
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