Keeping custom field in Sub-Task consistent and updated with field in parent

Hello,

I would like the field "Verteilung" in Sub-Task to always be up to date, because the same field in parent tends to change from time to time and we need them synced. All that would be easy with scripted field but the field is actually type Select List (single choice) for which there is no custom template, at least I didn't find anything on it so far on google. Is this possible at all?

Cheers,

Rok

2 answers

This widget could not be displayed.

I've actually tried this with a scripted field, and it's messy because you then have to search using two different fields, the source field for the parent, and the separate scripted field for the child.  Syncing can be accomplished using a script listener that looks for changes to the parent that include the field and then updates the children.  Here is code I use to sync two fields, a custom field called "Verified Version/s" and the normal "Fix Version/s" field.  The listener is on all events that could change the field... Issue Updated, Issue Resolved, etc.

package com.myorg.listeners


import org.apache.log4j.Category
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.project.version.Version
import com.atlassian.jira.config.SubTaskManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.event.type.EventDispatchOption
import java.util.ArrayList
import java.util.Collection

class SyncParentFixVerifiedVersionsToSubTask extends AbstractIssueEventListener 
{
	Category log = Category.getInstance(SyncParentFixVerifiedVersionsToSubTask.class);
	SubTaskManager subTaskManager = ComponentManager.getInstance().getSubTaskManager();
	IssueManager issueManager = ComponentManager.getInstance().getIssueManager();
	CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager();

	Boolean changed = false;

	@Override
	void workflowEvent(IssueEvent event) {
		try {
			if (subTaskManager.isSubTasksEnabled()) {
				Issue eventIssue = event.getIssue();
				Collection<Issue> subTasks = eventIssue.getSubTaskObjects();
				if ( !eventIssue.getIssueTypeObject().isSubTask() ) {
					List changeItems = event.getChangeLog().getRelated("ChildChangeItem");
					if( changeItems.any {it.get('field')=='Fix Version'} ) {
						changed = true;
						Collection<Version> fixVersions = new ArrayList<Version>();
						fixVersions = eventIssue.getFixVersions();
						if (!subTasks.isEmpty()) {
							subTasks.each {
								it.setFixVersions(fixVersions);
							}                     
						}
					}
					if( changeItems.any {it.get('field')=='Verified Version/s'} ) {
						changed = true;
						CustomField vvcf = customFieldManager.getCustomFieldObjectByName("Verified Version/s");
						Collection<Version> verifiedVersions = new ArrayList<Version>();
						verifiedVersions = eventIssue.getCustomFieldValue(vvcf);
						if (!subTasks.isEmpty()) {
							subTasks.each {
								it.setCustomFieldValue(vvcf,verifiedVersions);
							}
						}
					}
					if (changed) {
						subTasks.each {
							issueManager.updateIssue(event.getUser(), it, EventDispatchOption.ISSUE_UPDATED, false);
						}
					}
				}
			}
		}
		catch (ex) {
			log.debug "Event: ${event.getEventTypeId()} fired for ${event.issue} and caught by SyncParentFixVerifiedVersionsToSubTask"
			log.debug (ex.getMessage())
		}
	}
}
This widget could not be displayed.

@Jeremy Gaudet's answer is interesting, just in case it's not clear he uses the exact same custom field between parent and child, therefore you can use simple JQL to search for either parents or children.

In answer to the specific question, you could just return the string value of the option, and use the plain text template.

This is all depends what you are trying to achieve. If it's searching subtasks, I would use jql:

issueFunction in subtasksOf("Verteilung = Foo") (... and subtask clauses)

If it's about drawing attention to an attribute of the parent issue on a subtask, I would use a script field.

I decided to go for return the string value of the option and to use the plain text template. I know it should be easy, but I am beginner in Scriptrunner sadly, what am I doing wrong? //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// def Verteilung = getCustomFieldValue("Verteilung").toString() return Verteilung ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// PS: Verteilung field in the parent is Select List (single choice) type

I thought it was on the parent? You are looking for a field on the current issue there. issue.parentObject.getCustomField( cf ) Look in other questions for how to get a custom field by name using CustomFieldManager

In fact there is an example in Jeremy's answer.

Suggest an answer

Log in or Sign up to answer
Atlassian Summit 2018

Meet the community IRL

Atlassian Summit is an excellent opportunity for in-person support, training, and networking.

Learn more
Community showcase
Posted Wednesday in New to Jira

Are you planning to trial, or are currently trialling Jira Software? - We want to talk to you!

Hello! I'm Rayen, a product manager at Atlassian. My team and I are working hard to improve the trial experience for Jira Software Cloud. We are interested in   talking to 20 people planning t...

135 views 2 0
Join discussion

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