copy field value from parent to child using listeners on issue updated

Uday Kiran Raparthy September 10, 2020

i am trying to copy custom field value from parent to all child issues and using the below code 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue

def issue = event.issue
def field = ComponentAccessor.getCustomFieldManager().getCustomFieldObject('customfield_13202')
def parentMyFieldValue = issue.parentObject.getCustomFieldValue(field)
def changeHolder = new DefaultIssueChangeHolder();
field.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(field), parentMyFieldValue),changeHolder);

 

getting error as below

Log:

2020-09-10 16:59:14,964 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2020-09-10 16:59:14,965 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null
java.lang.NullPointerException: Cannot invoke method getCustomFieldValue() on null object
 at Script320.run(Script320.groovy:11)o 

 

1 answer

0 votes
Gustavo Félix
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 10, 2020

Hi @Uday Kiran Raparthy 
I think your code is correct, but something else is causing the problem.

This is where the null pointer happens
def parentMyFieldValue = issue.parentObject.getCustomFieldValue(field)

field is null, so it was never assigned in the previous line.

def field = ComponentAccessor.getCustomFieldManager().getCustomFieldObject('customfield_13202')

Is that the correct customfield id ? 
You can also try with getCustomFieldObjectByName("Your Custom Field Name") to see if you get the same error. 

If you do, I think your customField is not present in that project or in that particular issue type.

Hope this helps you.

Uday Kiran Raparthy September 11, 2020

Hi @Gustavo Félix 

i tried with getCustomFieldObjectByName("Your Custom Field Name") but still see same issue. and the  customField is present in that project or in that particular issue type.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue

def issue = event.issue
def field = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName('ProjectStatus')
def parentMyFieldValue = issue.parentObject.getCustomFieldValue(field)
def changeHolder = new DefaultIssueChangeHolder();
field.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(field), parentMyFieldValue),changeHolder);

Gustavo Félix
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 11, 2020

Ok, I just replicate the error.

Your are using a script listener, maybe with issue created? 
But every type of issue is entering there.

I have an if 
if(issue.issueTypeId.equalsIgnoreCase("10003")){
//your code
}

That way I'm sure the code is executed only when the issue type is a subtask.

Uday Kiran Raparthy September 11, 2020

Here, i'm using Issue Updated Event.

The Scenario is i'm trying to copy field value from feature to its child issues(Story's and Bug's)

Gustavo Félix
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 11, 2020

Ok, but what is getting updated to trigger the script listener?

If you update the feature, then It would execute your code and fail, because your feature doesnt have a parent.

Uday Kiran Raparthy September 11, 2020

updating the child issue(story) so that listener gets triggered and it copies value from parent feature

Gustavo Félix
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 11, 2020

The error is that your story doesnt have a parent.
How do you set that parent relationship on your story and on your bugs?

Uday Kiran Raparthy September 11, 2020

relationship was maintained through a custom-field "Feature link"

see the below screenshot, i was trying to update 'Projectstatus' field on the below child issues

Capture.PNG

Gustavo Félix
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 11, 2020

Ok, I dont know about Issue in Feature, but I think it's the same functionality as Epics, Issue in Epic.

this works in epics:

def epicLink = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Epic Link")

Issue epic = issue.getCustomFieldValue(epicLink) as Issue 
def parentValue = epic.getCustomFieldValue(epicLink)

If it has the same functionality, something like this would work for you
def featureLink = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Feature Link")

Issue parentFeature= issue.getCustomFieldValue(featureLink ) as Issue 

def parentValue = parentFeature.getCustomFieldValue(featureLink )

Suggest an answer

Log in or Sign up to answer