Copy customfield value from parent issue during creation with help of a Post-function

Dominic Feig August 7, 2013

Hello there,

currently I am using Jamie Echlin's Script Runner to replace the Misc Workflow Extensions plugin. With inspiration of the "transition parent" and "Copy custom field values" scripts I greated one for my own needs.

What I'd like to to: I want to create a sub task of an issue and I want to copy a customfield value from the parent issue to the new created sub task.

Here is my code:

package com.onresolve.jira.groovy.canned.workflow.postfunctions

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.config.ConstantsManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.util.ErrorCollection
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder

import com.onresolve.jira.groovy.canned.CannedScript

import com.opensymphony.workflow.loader.ActionDescriptor
import com.opensymphony.workflow.loader.StepDescriptor
import com.atlassian.jira.workflow.JiraWorkflow

import org.apache.log4j.Category

class CopyValueFromParent implements CannedScript{
	
	ComponentManager componentManager = ComponentManager.getInstance()

	Category log = Category.getInstance(CopyValueFromParent.class)

	public final static String CUSTOM_FIELD_ID = "CUSTOM_FIELD_ID"

	ComponentAccessor componentAccessor = new ComponentAccessor()

	def projectManager = componentAccessor.getRendererManager()

	CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()

	String getName() {
		return "Copy value from a parent issue"
	}

	String getDescription() {
		return """Copy value from a parent issue<br>
        """
	}

	List getCategories() {
		["Function"]
	}

	Integer getActionId(Issue issue, String customField) {
		JiraWorkflow workflow = componentManager.getWorkflowManager().getWorkflow(issue)
		StepDescriptor step = workflow.getLinkedStep(issue.status)
		ActionDescriptor ad = step.getActions().find {it.name == customField} as ActionDescriptor
		ad?.id
	}

	List getParameters(Map params) {
		[
			[
				Name:CUSTOM_FIELD_ID,
				Label:"Custom field",
				Description:"Choose the specific custom field you want to copy. E.g. 'customfield_10100'"
			]
		]
	}

	public ErrorCollection doValidate(Map params, boolean forPreview) {
		null
	}

	Map doScript(Map params) {
		log.trace ("TestCondition.doScript with params: ${params}");

		MutableIssue subtask = params['issue'] as MutableIssue
		
		log.trace ("subtask: $subtask")
		log.trace ("subtask.isSubTask(): ${subtask.isSubTask()}")
		
		
		if (subtask.isSubTask()) {
			MutableIssue parent = subtask.getParentObject() as MutableIssue

			CustomField cfParent = customFieldManager.getCustomFieldObject(params[CUSTOM_FIELD_ID] as String)
			CustomField cfSubtask = customFieldManager.getCustomFieldObject(params[CUSTOM_FIELD_ID] as String)

			Object parentFieldValue = parent.getCustomFieldValue(cfParent)
			Object subtaskFieldValue = subtask.getCustomFieldValue(cfSubtask)

			IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();

			ModifiedValue modifiedValue = new ModifiedValue(subtaskFieldValue, parentFieldValue)

			cfSubtask.updateValue( null, subtask, modifiedValue, changeHolder )

		}

		return params
	}

	String getDescription(Map params, boolean forPreview) {
		ConstantsManager constantsManager = ComponentManager.getInstance().getConstantsManager()

		StringBuffer sb = new StringBuffer()
		sb << getName() + "<br>Customfield " + params[CUSTOM_FIELD_ID]
		sb.toString()
	}

	public Boolean isFinalParamsPage(Map params) {
		true
	}
}


It would be great if someone could help me with my issue. Thanks in advance.

Edit: I updated the code I used. Unfortunately it doesn't work at all.

Edit2: This error occurs "2013-08-14 13:44:21,556 http-bio-8181-exec-3 ERROR dofe 824x55x1 nbhjci 0:0:0:0:0:0:0:1 /secure/QuickCreateIssue.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function

com.atlassian.jira.util.dbc.Assertions$NullArgumentException: issueId should not be null!"

3 answers

1 accepted

1 vote
Answer accepted
Dominic Feig August 18, 2013

I fixed the problem. The problem was the wrong setter method for the custom field value.

Map doScript(Map params) {

		String customField = params[CUSTOM_FIELD_ID] as String
		
		MutableIssue subtask = params['issue'] as MutableIssue
		
		log.trace ("subtask: $subtask")
		log.trace ("subtask.isSubTask(): ${subtask.isSubTask()}")
		
		
		if (subtask.isSubTask()) {
			MutableIssue parent = subtask.getParentObject() as MutableIssue

			CustomField cfParent = customFieldManager.getCustomFieldObject(params[CUSTOM_FIELD_ID] as String)
			CustomField cfSubtask = customFieldManager.getCustomFieldObject(params[CUSTOM_FIELD_ID] as String)

			Object parentFieldValue = parent.getCustomFieldValue(cfParent)
			Object subtaskFieldValue = subtask.getCustomFieldValue(cfSubtask)
			
			subtask.setCustomFieldValue(cfSubtask, parentFieldValue)

		}

		return params
	}

Maybe this will help others.

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.
August 18, 2013

I will shortly be releasing a version which includes a few functions to help people (myself mainly) migrate from JMWE. However this won't be included as I recommending using the version in jira suite utilities. The "copy field" version has an option to copy from parent to child, in a recent version.

Mehmet Kazgan
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.
January 5, 2014

Hi Jamie,

I am trying to copy field values from one issue to another. Any reference would be appreciated.

Thanks.

0 votes
Dominic Feig August 7, 2013

Unfortunately this doesn't fit to my need. But thanks anyway this can help me with a different problem I am not know now :-)

Concrete example: Someone creates an issue. Later on another or the same person is creating a sub task of the isse. And in new sub task it should inherit a customfield value from the parent issue.

0 votes
Tsol
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 7, 2013

Use create sub task post function and in additional action field add the script below

//create custom field object from parent
def cf_1 = customFieldManager.getCustomFieldObjectByName('CustomField_name')

//get value of custom field
val_1 = 'Test ' + issue.getCustomFieldValue(cf_1)


//Set subtask description for example
issue.setDescription(val_1)

Modify it so it fits the things you want to do.

Custom field confliguration should include subtask issue type.

Cheers

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events