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

how a display a hyperlink in a table in script field Edited

I followed the method in this wiki page to show a table in script field: https://scriptrunner.adaptavist.com/latest/jira/recipes/misc/table-custom-field.html

but the table just show text. I also want the issue key in the table is a hyperlink to be clicked. 

so I tried to return a html format of hyperlink, it can be displayed as a hyperlink

def hyperlink = "<a href=\""+browseurl+issue.key+"\">"+issue+"</a>";
log.info(hyperlink)
return hyperlink

but when I input this information into a table (as code below), it displays as text.

def results = searchProvider.search(query, user, PagerFilter.getUnlimitedFilter())

log.debug("Total issues: ${results.total}")

def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
def browseurl = com.atlassian.jira.component.ComponentAccessor.getApplicationProperties().getString("jira.baseurl")+"/browse/"

if (results.total < 1)
{
xml.string("Nothing required.")
}
else
{
xml.style(type: "text/css",
'''
#scriptField, #scriptField *{
border: 1px solid black;
}

#scriptField{
border-collapse: collapse;
}
''')

xml.table(id: "scriptField") {
tr {
th("Key")
th("Summary")
th("Status")
}
results.getIssues().each { documentIssue ->
log.debug(documentIssue.key)
// if you need a mutable issue you can do:
def issue_i = issueManager.getIssueObject(documentIssue.id)
// do something to the issue...
log.debug(issue_i.key)
//issue_i hyperlink
def ikey = issue_i.key;
def issue_i_link = "<a href=\""+browseurl+ikey+"\" >"+ikey+"</a>";
tr {
td(issue_i_link)
td(issue_i.summary.toString())
td(issue_i.status.getName())
}
}
}
}
return (writer.toString())

 what is displayed: 

<a href="https://xxx/xx">issue</a>

2 answers

1 accepted

0 votes
Answer accepted

You need to either build the URL using the markup syntax or use mkp.yieldUnescaped to have the html interpreted. Read more about the markup builder: http://groovy-lang.org/processing-xml.html#_markupbuilder 

xml.table(id: "scriptField") {
tr {
th("Key")
th("Summary")
th("Status")
}
results.getIssues().each { documentIssue ->
def issue_i = issueManager.getIssueObject(documentIssue.id )

def ikey = issue_i.key;
tr {
td{
a(href: "/browse/$ikey") ikey
}
td(issue_i.summary.toString())
td(issue_i.status.getName())
}
}
}

or

td( mkp.yieldUnescaped(issue_i_link) )

 BTW, if you want to create a link to jira to be displayed in jira...  and you don't have a context path in your environment, you don't need to lookup the base url, you can just use an absolute path starting at root : "/browse/ABC-123" will work. If you have a context path, you can either hard code that or look it up.

Also, with groovy, you can make strings lots easier to read by using triple quoted strings. With those, you don't need to escape quotes:

def longComplexString = """ this string has unescaped "double quotes" """

Thanks for your answer. that helps a lot!

Thanks for your answer. that helps a lot!

Suggest an answer

Log in or Sign up to answer
TAGS

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