Forums

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

Inherit value from parent

AgileHappyLearner
Contributor
February 10, 2023

I would like to create a scrip field in User Stories and Tasks to show the value introduced in their parent issue, in my case, the parent issue is a "Requirement" and the field with the information is named "SNOW Number". Could you help me with a script for this?, I know there are already plug ins to do that but I don't want to install another plug in. I already have Scrip Runner plug in installed.

Thanks!

2 answers

1 accepted

0 votes
Answer accepted
Nic Brough -Adaptavist-
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.
February 12, 2023

This script should do it.

Create a scripted field with a different name to "SNOW Number" ("All issues SNOW Number" for example)

Make sure you set the "template" for the custom field to the same type of field that your SNOW Number is (number or text), otherwise you'll need a bit more code to convert the output to the template type.

If you're going to be using this field for searching (like "show me all the issues and their sub-tasks with SNOW Number = 42"), then you will need to put the field on the parent issues, and replace the "if issue is not sub-task's "return" with code that returns the SNOW Number from the current issue.

 

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.IssueFieldConstants

import com.atlassian.jira.issue.fields.FieldManager

// the name of the field whose value we want to show on sub-task

final String fieldNameToCopy = "SNOW Number"

FieldManager fieldManager = ComponentAccessor.fieldManager

if (!issue.subTask) {

return

}

def fieldToCopy = fieldManager.allAvailableNavigableFields.find { it.name == fieldNameToCopy }

if (!fieldToCopy) {

log.info "Could not find field with name $fieldNameToCopy"

return

}

def parentIssue = issue.parentObject

def fieldToCopyId = fieldToCopy.id

def customField = ComponentAccessor.customFieldManager.getCustomFieldObject(fieldToCopyId)

return parentIssue.getCustomFieldValue(customField)


AgileHappyLearner
Contributor
February 12, 2023

Thanks for the information, I tried with your solution but It returns null all the time for issues that I know have information in SNOW Number field

Nic Brough -Adaptavist-
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.
February 12, 2023

Ok, find an issue that has a SNOW number, go into the script editor for the field, and try putting the issue key into the test box.  The tab beneath it will show you the execution log, including errors.

I'd add a bit more logging in:

if (!issue.subTask) {
log.info "Not a sub-task"
return
}
...
log.info customField
return parentIssue.getCustomFieldValue(customField)

Once we've found the problem, you'll want to remove those logs to avoid performance issues and filling your logs with unneeded lines.

0 votes
AgileHappyLearner
Contributor
February 12, 2023

I have been using this script

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.link.IssueLinkManager

def linkManager = ComponentAccessor.getIssueLinkManager()
def parentIssueType = "requirement"
def childIssueType = ["user story"]
def parentFieldName = "Service Now Number"
def childFieldName = "Req SNOW Number"
def parentIssue = getParentIssueByType(issue, parentIssueType)
def parentFieldValue = parentIssue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName(parentFieldName))

return parentFieldValue

private Issue getParentIssueByType(Issue issue, String parentIssueType) {
    def parentIssue = issue
    while (parentIssue.getParentObject() != null && !parentIssue.getIssueType().getName().equalsIgnoreCase(parentIssueType)) {
        parentIssue = parentIssue.getParentObject()
    }
    if (parentIssue.getIssueType().getName().equalsIgnoreCase(parentIssueType)) {
        return parentIssue
    }
    return null
}
But I receive the following error
java.lang.NullPointerException: Cannot invoke method getCustomFieldValue() on null object at Script5.run(Script5.groovy:13)
Some idea of where is the problem
Nic Brough -Adaptavist-
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.
February 12, 2023

The problem in your code is that you are getting the parent issue type, not the actual parent issue, but see my answer for a different way to do it.

Suggest an answer

Log in or Sign up to answer