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 do I default the value of a sub-task field to the value of a parent field?

When users create a sub-task, I want a custom field value on the sub-task to default to the value on the parent when the sub-task screen is displayed. How do I configure/enable/script this?

3 answers

1 accepted

2 votes
Answer accepted

Thanks for the suggestions, guys. I ended up solving this a little differently.

I originally had the field as required so I wanted the value set when the screen opened. After rethinking things, I made the field optional and used the JIRA Miscellaneous Workflow Functions plugin to create a post function that sets the field to the value from the parent.

have you considered the case where the parent is updated? should the child update as well?

Is this possible? The below code works if I update the Subtask, but not if I update the parent. If I change the parent, I want all subtasks to be updated.

geting/setting customfields can be a bit involved in java/groovy code, so you could use my add-on's configurable copy Listener for that: Slingshot Updatefields.

cheers, Jos

jos is right. created custom code can be time consuming so a plugin that speeds that along is ideal. if you hell bent on crafting the custom code though, i created a listener in groovy that ensures the fixversion of the children always matches the fixversion of the parent. you'll find it below if you care to revise to fit your needs.

package com.custom
 
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.IssueInputParameters
import com.atlassian.jira.security.JiraAuthenticationContext
import com.atlassian.jira.project.version.Version
import com.atlassian.jira.event.type.EventDispatchOption
import org.apache.log4j.Category
import com.atlassian.jira.issue.changehistory.ChangeHistoryManager
import com.atlassian.jira.issue.history.ChangeItemBean
import java.util.Calendar
import java.util.Date
import java.sql.Timestamp

class TaskVersionListener extends AbstractIssueEventListener {
    Category log = Category.getInstance(TaskVersionListener.class)
 
    @Override
    void issueUpdated (IssueEvent event) {
	log.setLevel(org.apache.log4j.Level.WARN)
	log.debug ("---- TaskVersionListener starts -----")
	log.debug ("issue: ${event.issue} || task: ${event.issue.isSubTask()}")
		
	// method configuration
	Calendar cal = Calendar.getInstance()
	cal.add(Calendar.SECOND, -30)
	Date date = cal.getTime()
	Timestamp threshhold = date.toTimestamp()
	IssueManager issueManager = ComponentAccessor.getIssueManager()
	UserManager userManager = ComponentAccessor.getUserManager()
	ChangeHistoryManager changeHistoryManager = ComponentAccessor.getChangeHistoryManager()

	if (!event.issue.isSubTask()) {
		List <ChangeItemBean> changeItemBeans = changeHistoryManager.getChangeItemsForField(event.issue,"Fix Version")
		changeItemBeans.each { item ->
			log.debug ("Fix Version created at ${item.getCreated()} with threshhold of $threshhold")
			if (item.getCreated().after(threshhold)) {
				log.debug ("green light for item above!")
				event.issue.getSubTaskObjects().each { t ->
					if (!t.getStatusObject().getName().equals("Done")) {
						MutableIssue myIssue = t
						myIssue.setFixVersions(event.issue.getFixVersions())
						issueManager.updateIssue(userManager.getUser("automation"), myIssue, EventDispatchOption.DO_NOT_DISPATCH, false)
					}
				}
			}
		}
	} else {
		List <ChangeItemBean> changeItemBeans = changeHistoryManager.getChangeItemsForField(event.issue,"status")
		changeItemBeans.each { item ->
			log.debug ("created at ${item.getCreated()} with threshhold of $threshhold")
			if (item.getCreated().after(threshhold) && item.getFromString().equals("Done")) {
				log.debug ("green light for item above!")
				MutableIssue myIssue = event.issue
				myIssue.setFixVersions(event.issue.getParentObject().getFixVersions())
				issueManager.updateIssue(userManager.getUser("automation"), myIssue, EventDispatchOption.DO_NOT_DISPATCH, false)
			}
		}
	}
	log.debug ("---- TaskVersionListener ends ------")
    }
}

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted in Jira

We want to hear from you! How do you use Slack and Atlassian together?

Hi Everyone! My name is Mina and I am on Atlassian’s Ecosystems Marketing team. Our team is focused on our technology partnerships and marketplace apps. One of Atlassian’s partners is Slack, who ...

24 views 0 2
Join discussion

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