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

Can someone please validate my script for updating subtask fix version ? Edited

I am looking at the option of using Script listener to update the sub task when the Fix version of the Parent is updated. Below is the script for it, my only problem with the script is, it does not copy the Fix Version of the parent when creating a sub task, but when I change the Fix version of parent later, then it works and reflects on the Sub Task.

EVENT - Issue created, Issue updated

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.project.version.Version

IssueManager issueManager = ComponentAccessor.getComponent(IssueManager.class)

Issue updatedIssue = event.getIssue()
Collection<Version> fixVersions = new ArrayList<Version>()
fixVersions = updatedIssue.getFixVersions()
Collection<Issue> subTasks = updatedIssue.getSubTaskObjects()
subTasks.each {
if (it instanceof MutableIssue) {
((MutableIssue) it).setFixVersions(fixVersions)
issueManager.updateIssue(event.getUser(), it, EventDispatchOption.ISSUE_UPDATED, false)
}
}

 

Can someone please help :) Thank you

2 answers

0 votes

It works correctly. In the script you look for subtasks, that is why your script works only for the parent task. Your scripts for the create subtask and update parent task must be different. For the create event you should get the parent of the issue, get the fix version and save it to the event issue.

@Alexey Matveev _cPrime_ Hello. Thanks for the comment. So, I need to create another listener to copy the Fix Version of parent when a sub task is being created ? 

You can either create another listener or use the same one. But you need to define in the listener whether the event issue is a sub-task or the parent task and then code different logic.

For parent task you would iterate over sub-tasks and set the fix version for the sub-tasks (that is what your script does right now)

For subtasks you would find the parent task, take the fix version and set the fix version for the sub-task.

@Alexey Matveev _cPrime_ Thank you. I will try adding another listener and see how it goes :) 

@Alexey Matveev _cPrime_

I came across the below script for a similar scenario. Would this work for my requirement. (Not from a developmental background, so had trouble writing one myself)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

if(!event.issue.isSubTask() && !event.issue.getSubTaskObjects()){
    return
}

def customFieldManager = ComponentAccessor.customFieldManager

if(event.issue.isSubTask()) {

    def parent = event.issue.getParentObject()
    def status = parent.status.name

    def parentStatus = customFieldManager.getCustomFieldObjectByName("Fix Version/s")
    def changeHolder = new DefaultIssueChangeHolder()

    parentStatus.updateValue(null, event.issue, new ModifiedValue(parentStatus.getValue(event.issue), status), changeHolder)
}

if(event.issue.getSubTaskObjects()){
    def subtasks = event.issue.getSubTaskObjects()
    def status = event.issue.status.name
    subtasks.each {
        def parentStatus = customFieldManager.getCustomFieldObjectByName("Fix Version/s")
        def changeHolder = new DefaultIssueChangeHolder()

        parentStatus.updateValue(null, it, new ModifiedValue(parentStatus.getValue(it), status), changeHolder)
    }

}

Try to log info into the atlassian-jira.log and have a look if it works as you expect. You can log with log.error("variable: " + variable)

@Alexey Matveev _cPrime_ Thank you. That script did not work. 

@Alexey Matveev _cPrime_ Hello Alexey, can you please validate the below script, (I am so sorry for troubling you so much)

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue

def field = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Fix Version")

//def field = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("fixVersion")

def parentMyFieldValue = issue.parentObject.getCustomFieldValue(field)
def changeHolder = new DefaultIssueChangeHolder();
field.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(field), parentMyFieldValue),changeHolder);

@Aisha M  did the above script worked ?

Suggest an answer

Log in or Sign up to answer
TAGS

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