Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,299,744
Community Members
 
Community Events
165
Community Groups

Read-Only custom Scripted field from Linked Issues

Edited

I am trying to create a scripted field in my cloud instance that looks up the Linked Issues for a specific link type and then outputs the key & summary into the field. 

Example. 

Epic TEST-12 is linked to ABC-123 using a "Initiative/Project" issue link. 

On ticket TEST-12 there is a field called "Initiative" and that field should show "ABC-123: Initiative XYZ" 

I had this working on server but now that I'm on a cloud instance I can't get the script to work with the changes between the plugins. 

Any help would be appreciated. Thank you!

 

Edit: This is the script from the server version of JIRA. Please note that this code DOES work in server. I am trying to convert it to Cloud.

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.issue.label.LabelManager

import com.atlassian.jira.issue.Issue

import org.apache.log4j.*

// Configure logger

def log = Category.getInstance("com.domain.jira.automation")

log.setLevel(Level.DEBUG)

// log.debug "debug statements"

// Get necessary managers

def cfm = ComponentAccessor.customFieldManager

def issueManager = ComponentAccessor.issueManager

// Link Type IDs can be found by inspecting HTML on the Issue Linking page: https://jira.domain.com/secure/admin/ViewLinkTypes!default.jspa

def linkTypeId_OutcomeDeliverable = 11101

List<Issue> getLinkedIssuesByLinkTypeId(Issue issue, Long linkTypeId) {

//Go through all linked issues and collect their keys if link is of specified type

def linkManager = ComponentAccessor.issueLinkManager

List<Issue> foundIssues = []

linkManager.getInwardLinks(issue.id).each{

def linkedIssue = it.sourceObject

if (it.linkTypeId == linkTypeId)

{

foundIssues.add(linkedIssue)

}

}

log.debug "found "+(foundIssues.size())+" linked issues of that type"

foundIssues

}

def initiatives = getLinkedIssuesByLinkTypeId(issue, linkTypeId_OutcomeDeliverable)

def initiativeKeys = initiatives.collect { it.key }.join(', ')

def initiativeSummary = initiatives.collect { it.summary }.join(', ')

log.debug "found initiatives: ${initiativeKeys}"

return "${initiativeKeys}: ${initiativeSummary}"

1 answer

1 accepted

1 vote
Answer accepted

Ah, searching Google for: getLinkedIssuesByLinkTypeId cloud

Brought up, (of course) @Ravi Sagar _Sparxsys_ 's video on something related:

Now, that's a Post-function, but as you can see just through a little of the code, it's very different calls (having to talk to the API) to find linked issues. 

Because I was thinking - Scriptrunner on Cloud is very different from Server.

To wit:

Scriptrunner Server vs Cloud

Darryl Lee Community Leader Oct 15, 2021

Oh man, now you've done it. I went and learned the tiniest bit of Groovy on Jira Cloud:

def links = get('/rest/api/3/issue/' + issue.key)
.header('Content-Type', 'application/json')
.asObject(Map)

// issueType IDs can be found by inspecting HTML on the Issue Linking page: https://YOURSITE.atlassian.net/jira/settings/issues/issue-types

def issueTypeId_OutcomeDeliverable = "11101"

//Go through all inbound links and collect their keys if link is of specified type

def initiatives= []

links.body.fields.issuelinks*.inwardIssue.each{

if(it.fields.issuetype.id == issueTypeId_OutcomeDeliverable)
{
initiatives.add(it)
}

}

def initiativeKeys = initiatives.collect { it.key }.join(', ')
def initiativeSummary = initiatives.collect { it.fields.summary }.join(', ')

//return initiatives
return "${initiativeKeys}: ${initiativeSummary}"

You'll want to make sure your Issue Type ID for Initiatives on your Cloud instance is 11101.

Thank goodness for @Ravi Sagar _Sparxsys_'s source code here, and this old script from @Aidan Derossett [Adaptavist] that showed me I could do this in a simpler way without Lists, which I don't fully understand because I AM NOT A PROGRAMMER.

Anyways so yeah, that was some definition of fun. As I look at the final outcome though, I'm not quite sure what the point of this field is, since it replicates information in the Linked issues section:

Screen Shot 2021-10-15 at 2.09.50 AM.png

Like # people like this

:) I am glad it was useful.

Like Dave Rosenlund _Tempo_ likes this

@Darryl Lee You need this field for sending the data to roadmapping tools and for listing as a column in Structure. That's the whole reason it was created in the first place. 

THANK YOU SO MUCH!!!!

Like Dave Rosenlund _Tempo_ likes this
Darryl Lee Community Leader Oct 15, 2021

Hrm. Does it actually work in Structure and whatever roadmapping tool you're using? My understanding was that Script Fields are a little... weird.

To wit:

In Cloud, Scripted Fields are not stored as normal Jira custom fields. Instead, the field output is added to an issue’s properties and therefore inherits the issue’s permissions. Scripted Fields are not currently searchable with JQL, but we’re working hard to make this available as soon as possible.

https://docs.adaptavist.com/sr4jc/latest/scriptrunner-migration/feature-parity

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence

An update on Confluence Cloud customer feedback – June 2022

Hi everyone, We’re always looking at how to improve Confluence and customer feedback plays an important role in making sure we're investing in the areas that will bring the most value to the most c...

316 views 2 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you