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

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
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

Mission-critical battery manufacturer fulfills FAA software requirements with Commit Policy Plugin

EaglePicher Technologies is a leading manufacturer of battery systems for diverse industries like defense, aviation, space or medical. As they operate in highly regulated industries, keeping a clear ...

177 views 0 2
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you