Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Scriptrunner: Scripted Field to show Attachments from Linked Issues

Hi,

I am attempting to use a Scripted Field to list all of the Attachments from all Linked issues.

I'm not sure how to get the information from the Linked Issues.

Here is the Code:

import com.atlassian.jira.issue.attachment.Attachment;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.bc.project.ProjectService;
def commentManager = ComponentAccessor.commentManager
def projectService = ComponentAccessor.getComponent(ProjectService)
def attachmentManager = ComponentAccessor.getAttachmentManager()
def numberAttachments = attachmentManager.getAttachments(issue).size()
String s = "";
if(null != attachmentManager.getAttachments(issue))
{
for(Attachment att : issue.attachments)
{
s = s +"<a href=\"" + "/secure/attachment/"+att.id+"/"+att.filename+"\">"+att.filename+"</a>" + "<br>";
}
}
return s;

Here is a Mock up Of what I would like to show in the Main Ticket. The field will show Clickable URL links to all attachments from the linked issues

 

Scripted Fields Attachments on Linked Issues.png

 

Thanks!

1 answer

1 accepted

3 votes
Answer accepted

Something like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue

def issueLinkManager = ComponentAccessor.getIssueLinkManager()

def issues = [issue] as List<Issue>
issues.addAll(issueLinkManager.getOutwardLinks(issue.id).collect {it.destinationObject})
issues.addAll(issueLinkManager.getInwardLinks(issue.id).collect {it.sourceObject})

issues.collect { issue ->
    issue.getAttachments().collect { att ->
        "<a href=\"" + "/secure/attachment/" + att.id + "/" + att.filename + "\">" + att.filename + "</a>" + "<br>"
    }
}.flatten().join("")

 

I think there is a bit more to do though... permissions checking, also attachments can have chars in their filename that are not valid in URLs, etc.

Thank you,

Is there a way to restrict the Link Display Name? We have created a custom link name SD-Blocking-Link for both ways Outward and InWard.

It will only show links with the custom name SD-Blocking-Link

I changed the script to the following -

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.fields.IssueLinksSystemField
import com.opensymphony.workflow.InvalidInputException
import webwork.action.ActionContext
def request = ActionContext.getRequest()
def params = request.getParameterMap()
def fieldManager = ComponentAccessor.getFieldManager()
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def linksSystemField = fieldManager.getField("issuelinks") as IssueLinksSystemField
def issueLinkingValue = linksSystemField.getRelevantParams(params) as IssueLinksSystemField.IssueLinkingValue
def issues = [issue] as List<Issue>
issues.addAll(issueLinkManager.getOutwardLinks(issue.id).collect {it.destinationObject})
issues.addAll(issueLinkManager.getInwardLinks(issue.id).collect {it.sourceObject})
if (! (issueLinkingValue.linkDescription == "SD-Blocking-Link" && issueLinkingValue.linkedIssues.size() > 0))
{issues.collect { issue ->
    issue.getAttachments().collect { att ->
        "<a href=\"" + "/secure/attachment/" + att.id + "/" + att.filename + "\">" + att.filename + "</a>" + "<br>"
    }
}.flatten().join("")}

 

The issue is, when it runs I am getting the following error:

java.lang.NullPointerException: Cannot invoke method getParameterMap() on null object

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
def issueLinkManager = ComponentAccessor.getIssueLinkManager()
def issues = [issue] as List<Issue>
issues.addAll(issueLinkManager.getOutwardLinks(issue.id).findResults {
    if (it.issueLinkType.name == "SD-Blocking-Link") {
        return it.destinationObject
    }
    null
})
issues.addAll(issueLinkManager.getInwardLinks(issue.id).findResults {
    if (it.issueLinkType.name == "SD-Blocking-Link") {
        return it.sourceObject
    }
    null
})
issues.collect { issue ->
    issue.getAttachments().collect { att ->
        "<a href=\"" + "/secure/attachment/" + att.id + "/" + att.filename + "\">" + att.filename + "</a>" + "<br>"
    }
}.flatten().join("")
Like Mihai Mihai likes this

Thank you!

When I try using the above code to test something out I get an error shown on the issues.collect line which says:

expecting '>', found '-' @line 17, column 24.

Any ideas why?

Hi @JamieA 

 

The last script you posted here also looks at the attachments of the current issue and displays them. Is it possible to show just the attachments of the linked issues?

 

Thank you!

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira Service Management

JSM June Challenge #2: Share how your business teams became ITSM rockstars

For JSM June Challenge #2, share how your non-technical teams like HR, legal, marketing, finance, and beyond started using Jira Service Management! Tell us: Did they ask to start using it or...

314 views 9 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