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!!

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 Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Julia Dillon
Posted Tuesday in Jira

Tell us how your team runs on Jira!

Hey Atlassian Community! Today we are launching a bunch of customer stories about the amazing work teams, like Dropbox and Twilio, are doing with Jira. You can check out the stories here. The thi...

172 views 1 17
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