Custom field text to clickable URL.

KRC September 20, 2017

Hi

I have a requirement as following

The user will enter a number in the custom field which is validated to enter number only while creating a ticket. Once ticket gets created I need that number to get added to the URL so that user clicks that link and it takes to the appropriate page. 

Eg: In a different enterprise system, there is a numeric id for a ticket, and that numeric id is entered in Jira, I need JIRA custom field which enables users to input that number and that field data should convert into URL like http://abcdef.com/nbvj/1234 in ticket view  so that user gets direct to that page when clicked.

Am new to JIRA scripting and am not sure where to add the script and what to code. I'm a beginner so any help is appreciated

 

Thank you

3 answers

1 accepted

3 votes
Answer accepted
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 20, 2017

As I think you have already worked out, I would probably want to do this as a "scripted field" in ScriptRunner.

There's quite a lot to do here for a first-time user, so I will try to be brief, and expect questions if I am too brief, or assume things you don't know!

  • Add a new custom field
    • Type: numeric
    • Name: Let's call it "Reference entry"
    • Place it on create and edit screens.  You might find it useful on view, but I'm not sure.
  • Add a second custom field
    • Type: Scripted field
    • Name: Let's say "Reference url"
    • Place it on the view screen.  (It won't show on create/edit if you add it, but don't worry if you do)
  • Now, go to Admin -> ScriptRunner -> Scripted fields
  • You will find a field in there called "Reference url"
    • Set it up to have an output type of "html"
    • Give it the following script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
CustomField customField = customFieldManager.getCustomFieldObjectByName("Reference entry")
 
def theId = (String) issue.getCustomFieldValue(customField)

if (theId)
{
   return '<a href="http://abcdef.com/nbvj/' + theId + '>See item ' + theId + '</a>'
}

return null

 

KRC September 21, 2017

Thank you for the answer.I have couple questions, here it goes

 

In the line

def theId = (String) issue.getCustomFieldValue(customField), I think I should use custom field number of(reference URL) there in order to pull the data, am I correct? or field being scripted field it automatically pulls data?

and what is See item in return statement? maybe a dumb question but trying to understand the code.

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 21, 2017

A scripted field is aware of the issue it is running for.  You don't need to think about it, you can just use the issue object like my code does.

The customField is worked out by the line above it in the script.

The "see item" is some text I added as an anchor for the url field, so you see something like "See item: 12345" instead of http://abcdef.com/nbvj/12345 - totally optional and cosmetic.

KRC September 21, 2017

ah. understood.

 

Thank you

1 vote
Ольга Золомихина August 10, 2018

Your quotation marks are wrong, it shoul be something like that:

return '<a href=http://abcdef.com/nbvj/'+ theId +'>'+"Seeitem:"+theId +'</a>'

0 votes
9103296 July 31, 2023

Hi @Nic Brough -Adaptavist- 

I have the same exact scenario to be achieved in JIRA cloud. Could you please guide me in providing the script for scripted field

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 31, 2023

Suggest an answer

Log in or Sign up to answer