ScriptRunner Listener to copy Parent link to new custom text field called Portfolio Parent Link

SMAtlassianMber
Contributor
June 4, 2019

Due to the reporting restriction on portfolio fields we are having to copy the parent link value into a standard text field so that we can report off of parent link. Below is the start of a listener I am working on, but can't get it to work. Any suggestions? 

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.index.IssueIndexingService;
import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.acme.LISTENER")

log.setLevel(Level.DEBUG)

def issue = event.issue as Issue

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def parentLink = customFieldManager.getCustomFieldObjectByName("Parent Link")
def value = issue.getCustomFieldValue(parentLink)
def parentKey = value.key
def parentIssue = ComponentAccessor.issueManager.getIssueByCurrentKey(parentKey)
def portParentLink = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Portfolio Parent Link")
def changeHolder = new DefaultIssueChangeHolder()
def issueIndexManager = ComponentAccessor.getComponent(IssueIndexingService)

if (parentIssue == null)
{
log.debug ("Parent link value is null: End listener")
}
else
if (parentIssue != Null)
{
portParentLink.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(parentIssue),changeHolder)
}
issueIndexManager.reIndex(issue)
}

2 answers

0 votes
SMAtlassianMber
Contributor
June 4, 2019

I figured out what was wrong and I have updated the script. now it is working.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.index.IssueIndexingService;
import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("com.acme.ParentLinkLISTENER")

log.setLevel(Level.DEBUG)

def issue = event.issue as Issue

log.debug "Test Debug"

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def parentLink = customFieldManager.getCustomFieldObjectByName("Parent Link")
def parentLinkValue = issue.getCustomFieldValue(parentLink)
def portParentLink = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Portfolio Parent Link")
def changeHolder = new DefaultIssueChangeHolder()
def issueIndexManager = ComponentAccessor.getComponent(IssueIndexingService)

if (parentLinkValue == null)
{
log.debug "Parent link is null: End of Script"
}
else
{
log.debug "Parent link used: Update Porfolio Parent Link"
log.debug("Child Issue = " + issue)

def key =(String)parentLinkValue.getKey();
def summary =(String)parentLinkValue.getSummary();

log.debug("Parent Link = " + parentLinkValue + summary)

portParentLink.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(portParentLink),key + " "+ summary),changeHolder)
}

 

0 votes
Ben Poulson
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.
June 4, 2019

What aspect of it doesn't work? If you just run the code in the console, does it work? Does it produce the correct output? Does it update the issue?

Suggest an answer

Log in or Sign up to answer