Add dynamic link based on other field value

We need to provide links to a different system based on the value of a field on each issue. The particular use case is to link the atlassian jira issues to work items in our TFS repository.

I want to be able to have a link on each issues that concatenates our tfs url (ex https://tfshost/tfs?id=) and the value of the TFS Work Item field (ex 123) so that the end result is a clickable link that sends the user to http://tfshost/tfs?id=123.

Any ideas?

2 answers

1 accepted

Hi Felix,

I hate to say it every time, but unfortunately I am afraid this type of customizations are to advanced for a jira ondemand instance. You need at least jira downloaded so you can install plugins like Scriptrunner or upload your own plugins with custom functionality.

Hi Felix,

Although the solution cannot be implemented in JIRA on-demand, I think it is still interesting to share my solution:

1) Install the Scriptrunner plugin

2) Create a custom field named "TFS Work Item" of type Text Field (or any type you want)

3) Create a custom field named "TFS Url" of type Scripted Field (Installed by the scriptrunner plugin)

4) Goto Admin > Addons > Scripted Fields and locate your scripted custom field. Next click the Edit link to open edit mode. In the script field paste the following code:

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue; 
 
 
CustomFieldManager customFieldManager = ComponentManager.getInstance().getCustomFieldManager(); 
CustomField tfsCustomField = customFieldManager.getCustomFieldObjectByName( "TFS Work Item" ); 

String url = "http://tfshost/tfs?id=" + issue.getCustomFieldValue(tfsCustomField);

return "<a href=\"" + url + "\">" + url + "</a>";

and make sure the HTML template is selected. This renders the value returned by the script as HTML (in this case a hyperlink).

5) Add the TFS Url to all the screens were it should be visible

This will be the result:

The advantage of using the Scriptrunner plugin is that the scripted field will be updated automatically whenever an issue field is updated. Changing the TFS Work Item field also updates the url.

This script does not check for null-values, but that test can be easily added :-)

Hope this helps?!

If someone would like to use Benjis code on JIRA 7, you have to modify it as shown below:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue;

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();
CustomField tfsCustomField = customFieldManager.getCustomFieldObjectByName( "TFS Work Item" );

String url = "http://tfshost/tfs?id=" + issue.getCustomFieldValue(tfsCustomField);

return "<a href=\"" + url + "\">" + url + "</a>";

#ComponentAccessor

best regards,
Anton 

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Monday in Jira Software

How large do you think Jira Software can grow?

Hi Atlassian Community! My name is Shana, and I’m on the Jira Software team. One of the many reasons this Community exists is to connect you to others on similar product journeys or with comparabl...

688 views 6 13
Read article

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