Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Update all sub tasks' custom field when a custom field is updated on its parent issue

NBE September 16, 2018

Hello,

Our "Story" issue type has a custom field named "Scheduled Release". Whenever this field is updated, I'd like it to be synced into all of the issue's sub tasks (the issue also has custom sub tasks types) which in turn also have a custom field named "Scheduled Release". I figured I should have a custom listener with the "CustomFieldUpdatedEvent" event, but I can't figure out the exact inline script I need to add.

Any help will be much appreciated.

Thank you!

Nir

2 answers

0 votes
Orkun Gedik
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.
September 17, 2018

Hello @NBE,

I wrote a script for you. I'd appreciate it if you let me know the result after trying. Feel free to ask if you have question about the code. Hope this helps.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.field.CustomFieldUpdatedEvent
import com.atlassian.jira.issue.Issue

if(event.getCustomFieldId() == "Scheduled Release ID" && issue.getIssueTypeId() == "story issuetype id"){
def subtasks = issue.getSubTaskObjects()
def customfield = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("Scheduled Release ID")
def scheduledRelease = customfield.getValue(issue)

for (Issue subtask : subtasks){
customfield.updateValue(null, subtask , new ModifiedValue(subtask.getCustomFieldValue(customfield), scheduledRelease), new DefaultIssueChangeHolder());
}
}

 

Regards,

Orkun Gedik

NBE September 19, 2018

Hi @Orkun Gedik

Thank you very much for taking the time to help me. 

Pasting your code into the inline script I get a couple of errors, all of them in this line: 

customfield.updateValue(null, subtask , new ModifiedValue(subtask.getCustomFieldValue(customfield), scheduledRelease), new DefaultIssueChangeHolder());
  1. unable to resolve class ModifiedValue
  2. unable to resolve class DefaultIssueChangeHolder

So I tried importing these classes:

  1. Importing com.atlassian.jira.issue.util.DefaultIssueChangeHolder fixed the 2nd problem.
  2. Importing com.atlassian.jira.issue.ModifiedValue not only did not fix the problem, but also caused a whole lot more errors in other places in the code (can you see it in your jira instance?)

Your help is much appreciated.

Nir

Orkun Gedik
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.
September 19, 2018

You are welcome  @NBE, can you share your error logs please.  

NBE September 19, 2018

@Orkun Gedik

No error logs... it fails on the validation of the inline script. 

 

See screenshot here: https://www.screencast.com/t/A4c4mNd9g

 

Thanks again! 

Orkun Gedik
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.
September 20, 2018

In the screenshot, imports are missing. Please import DefaultIssueChangeHolder  and ModifiedValue then share screenshot again. 

Orkun Gedik
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.
September 20, 2018

Can you add the line below, just after the imports 

Issue issue = event.issue
NBE September 20, 2018

@Orkun Gedik

It looks better now, but there's still an error: https://www.screencast.com/t/jGnwyDCurL6G

NBE September 20, 2018

@Orkun Gedik

Also, I tried running it without the if line, as shown below. In this case the script runs properly, but it does not execute what it should, i.e. - when I update a story's "Scheduled Release" field, it does not copy the value to all this story's sub tasks. Their "Scheduled Release" field remains empty. 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.field.CustomFieldUpdatedEvent
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

Issue issue = event.issue

//if(event.getCustomFieldId() == "Scheduled Release ID" && issue.getIssueTypeId() == "story issuetype id"){
def subtasks = issue.getSubTaskObjects()
def customfield = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("Scheduled Release")
def scheduledRelease = customfield.getValue(issue)

for (Issue subtask : subtasks){

customfield.updateValue(null, subtask , new ModifiedValue(subtask.getCustomFieldValue(customfield), scheduledRelease), new DefaultIssueChangeHolder());
}
//} 

 

 

Orkun Gedik
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.
September 20, 2018

getCustomFieldObject() method needs customfield id like 

getCustomFieldObject("customfield_1234")

Do not pass the paramater as a customfield name.

NBE September 27, 2018

@Orkun Gedik Thank you very much for your help. It's working perfectly. 

Orkun Gedik
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.
September 27, 2018

You are welcome :)

0 votes
Rambabu Patina _Appfire_
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.
September 16, 2018

Hi @NBE

Please have a look on similar article and try substituting with your custom field.

Thanks,
Ram.

Suggest an answer

Log in or Sign up to answer