How do I default the value of a sub-task field to the value of a parent field?

Peter Callies
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.
April 4, 2013

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
Peter Callies
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.
April 4, 2013

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.

Tanner Wortham
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.
April 4, 2013

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

Moriah Chandler
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.
August 26, 2014

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.

0 votes
Tanner Wortham
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.
April 4, 2013

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 ------")
    }
}

0 votes
Jos Braaksma April 4, 2013

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

Suggest an answer

Log in or Sign up to answer