Post Function Groovy - Updating CustomField in parent Jira from the CustomField in the sub-task

Andrew Lee March 31, 2014

Hi

I am trying to pass the value from a custom field in a sub-task screen to the custom field in its parent Jira. I have done some research and following is what I have.

I am not getting errors when I run the script via ScriptRunner. However, when I added the script to the post function for workflow, the custom field is not updated.

Would you be able to point out what I am missing.. Thank you for any help in advance!!

import org.apache.log4j.Category
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.opensymphony.workflow.InvalidInputException


MutableIssue mutableIssue = issue;
MutableIssue parentIssue = mutableIssue.getParentObject();

ComponentManager componentManager = ComponentManager.getInstance();
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();

IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();

String customFieldName = "customfield_11112";
CustomField cf = customFieldManager.getCustomFieldObject(customFieldName);

String parentCustomFieldValue = parentIssue.getCustomFieldValue(cf).toString();
String subTaskCustomFieldValue = mutableIssue.getCustomFieldValue(cf).toString();

//cf.updateValue(null, parentIssue, new ModifiedValue(parentIssue.getCustomFieldValue(cf), mutableIssue.getCustomFieldValue(cf)), changeHolder);
cf.updateValue(null, parentIssue, new ModifiedValue(parentIssue.getCustomFieldValue(cf), "test"), changeHolder);

//IssueIndexManager issueIndexManager = ManagerFactory.getIndexManager();
//issueIndexManager.reIndex(issue);

2 answers

2 votes
Andrew Lee April 1, 2014

Okay, no wonder... I haven't been publishing the draft after adding the script to the transition - first time to admin Jira... sigh.

It all solved my problem!! Thanks for your help!!

0 votes
EddieW
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.
March 31, 2014

You likely need to manually reindex the parent issue.

By default workflows issue a Reindex on the current issue/ BUt if your maniulating others, they will need a reindex as well.

Andrew Lee April 1, 2014

Thank you for reply!!
I have tried the following script by adding the "reIndex"... still couldn't quite get it.. actually I have a feeling that the script may not even be run properly but I think I have added the script for the correct transition.

Following is the updated script....


import org.apache.log4j.Category
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.issue.index.IssueIndexManager
import com.atlassian.jira.util.ImportUtils;


MutableIssue mutableIssue = issue;
MutableIssue parentIssue = mutableIssue.getParentObject();

ComponentManager componentManager = ComponentManager.getInstance();
CustomFieldManager customFieldManager = componentManager.getCustomFieldManager();

IssueChangeHolder changeHolder = new DefaultIssueChangeHolder();

String customFieldName = "customfield_11112";
CustomField cf = customFieldManager.getCustomFieldObject(customFieldName);

String parentCustomFieldValue = parentIssue.getCustomFieldValue(cf).toString();
String subTaskCustomFieldValue = mutableIssue.getCustomFieldValue(cf).toString();

parentIssue.setCustomFieldValue(cf, "test");


IssueIndexManager indexManager = ComponentManager.getInstance().getIndexManager();
Boolean wasIndexing = ImportUtils.isIndexIssues();
ImportUtils.setIndexIssues(true);

cf.updateValue(null, parentIssue, new ModifiedValue(parentIssue.getCustomFieldValue(cf), "test"), changeHolder);

indexManager.reIndex(parentIssue);
ImportUtils.setIndexIssues(wasIndexing);

Gavin Dodd March 8, 2016

I'm trying to get this to work in my workflow with little success. sad

Are you replacing "test" with the field name, or with the custom field id of the field within the parent?

Also, do you keep the "" or remove them?  Many thanks, Gav

Suggest an answer

Log in or Sign up to answer