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

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

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
Eddie Webb Community Champion Mar 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.

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);

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 Join to answer
Community showcase
Sarah Schuster
Posted Jan 29, 2018 in Jira

What are common themes you've seen across successful & failed Jira Software implementations?

Hey everyone! My name is Sarah Schuster, and I'm a Customer Success Manager in Atlassian specializing in Jira Software Cloud. Over the next few weeks I will be posting discussion topics (8 total) to ...

2,938 views 12 18
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
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot