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

Rok Antolic December 1, 2015

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

1 vote
JamieA
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.
December 1, 2015

@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.

Rok Antolic December 1, 2015

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

Jamie Echlin _ScriptRunner - The Adaptavist Group_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
December 2, 2015

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

Jamie Echlin _ScriptRunner - The Adaptavist Group_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
December 2, 2015

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

1 vote
Jeremy Gaudet
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.
December 1, 2015

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

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events