Forums

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

Update customField value of sub-task with parent task custom field value Script Listeners

Rafael Moreira October 22, 2018

Hi,

I want to update a custom field value in a sub-task if in the parent task the custom field value was updated, with Script Listeners.

The sub-task custom field and task custom field are the same field.

Thank you for your help.

1 answer

1 accepted

0 votes
Answer accepted
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.
October 22, 2018

Hello @Rafael Moreira,

 

You can create a script listener with IssueUpdated event and copy the code below. Hope this helps. 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.user.ApplicationUser


String cfName = "CustomField name here" // Change this
Issue currentIssue = event.issue
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def change = event?.getChangeLog()?.getRelated("ChildChangeItem")?.find {it.field == cfName}

if(change) {
CustomField cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName(cfName)
Collection<Issue> subtasks = currentIssue.getSubTaskObjects();

for(MutableIssue subtask : subtasks){
subtask.setCustomFieldValue(cf, cf.getValue(currentIssue))
ComponentAccessor.getIssueManager().updateIssue(user, subtask, EventDispatchOption.DO_NOT_DISPATCH, false)
}
}

Do not forget to change cfName value with the customfield name you want.

 

Kind Regards.  

Orkun Gedik

Rafael Moreira October 22, 2018

Hi,

Thank you for your answer, but I don't understand why I have this two errors that you can see in the print screen.

 

CaptureJIRa.PNG

Can you help me please ?

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.
October 22, 2018

Hi @Rafael Moreira,

 

Sorry for typo. Can you try to change 

getCustomFieldObjectsByName()

with 

getCustomFieldObjectByName()
Rafael Moreira October 22, 2018

Hi @Orkun Gedik

I still have one error

CaptureJIRAa.PNG

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.
October 22, 2018

Can you update the for loop as follows 

for(Issue issue : subtasks){
MutableIssue subtask = (MutableIssue) issue
subtask.setCustomFieldValue(cf, cf.getValue(currentIssue))
ComponentAccessor.getIssueManager().updateIssue(user, subtask, EventDispatchOption.DO_NOT_DISPATCH, false)
}
Rafael Moreira October 22, 2018

Hi @Orkun Gedik

Thank you very much, for your help.

Best regards

Suggest an answer

Log in or Sign up to answer