Custom field text to clickable URL. Edited

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

2 answers

1 accepted

This widget could not be displayed.

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

 

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.

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.

ah. understood.

 

Thank you

This widget could not be displayed.

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

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

Suggest an answer

Log in or Sign up to answer
Community showcase
Posted yesterday in Jira

Atlassian Research Workshop opportunity on Sep. 28th in Austin, TX

We're looking for participants for a workshop at Atlassian! We need Jira admins who have interesting custom workflows, issue views, or boards. Think you have a story to sha...

59 views 2 3
Join discussion

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you