Create a pre-filled link type

Rudy Holtkamp August 27, 2014

Hi,

Our documents are stored in a document management system which can be reached via an html link. Such a link looks like: http://company.com/webclient?argument=<NUMBER>

Is is possible to add a link type (like the default 'Jira issue' and 'Web link' to the link screen, in which the user only has to fill in the <NUMBER> and the rest of the URL is added automatically?

How do I do this?

Cheers,
Rudy

2 answers

0 votes
Rudy Holtkamp October 2, 2014

Well NicI've been trying to get it to work, but it keeps giving me errors, about the user (especially this part getRemoteIssueLinkByGlobalId(user, missue, globalId); ) 

javax.script.ScriptException: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'arholtkam:1' with class 'com.atlassian.jira.user.BridgedDirectoryUser' to class 'com.atlassian.jira.user.ApplicationUser'

So basically I can't get it to work. I'm not a Java/Groovy developer, so I'm just hacking around.

What I want is to use two fields to create a link. One field is the document number and the other one the type of document. Together they give me enough info to create the link.

Hope anyone can help me.

 

import com.atlassian.jira.security.JiraAuthenticationContext;
import com.atlassian.jira.component.ComponentAccessor;
//import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.link.RemoteIssueLink;
import com.atlassian.jira.bc.issue.link.RemoteIssueLinkService;
import com.atlassian.jira.issue.link.RemoteIssueLinkBuilder;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.ModifiedValue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder;
import com.atlassian.jira.issue.IssueImpl;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.user.BridgedDirectoryUser;
MutableIssue missue = issue;
//JiraAuthenticationContext authContext = ComponentAccessor.getJiraAuthenticationContext();
//User user = authContext.getLoggedInUser();
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser();
CustomFieldManager cfManager = ComponentManager.getInstance().getCustomFieldManager();
CustomField cfTCEDoc = cfManager.getCustomFieldObjectByName("Add TCE document");
CustomField cfTCEDocType = cfManager.getCustomFieldObjectByName("TCE document type");
//Get document number
def TCEDoc = issue.getCustomFieldValue(cfTCEDoc);
//Get document type
def TCEDocType = issue.getCustomFieldValue(cfTCEDocType);
//check if 'Add TCE document' value is set
if (TCEDoc == null) {return "NOK";}
//Check if type is specified
if (TCEDocType == "None") {return "NOK";}
//Create global id
def globalId = issue.getKey() + "-" + TCEDoc;
//First get a list of issue links to check if the link already exists
RemoteIssueLink existingLink = getRemoteIssueLinkByGlobalId(user, missue, globalId);
if (existingLink == null) {
//The link does not exist yet

RemoteIssueLinkBuilder remoteIssueLinkBuilder = new RemoteIssueLinkBuilder();
remoteIssueLinkBuilder.issueId(issue.getId());
remoteIssueLinkBuilder.relationship("TCE");
remoteIssueLinkBuilder.title(TCEDocType);
def url = "http://tce/tc/webclient?argument=" + TCEDoc;
remoteIssueLinkBuilder.url(url);
remoteIssueLinkBuilder.iconUrl("http://tce/images/navigatorapplication_24.png");
RemoteIssueLink remoteIssueLink = remoteIssueLinkBuilder.build();
RemoteIssueLinkService remoteIssueLinkService = ComponentManager.getComponentInstanceOfType(RemoteIssueLinkService.class);
RemoteIssueLinkService.CreateValidationResult createValidationResult = remoteIssueLinkService.validateCreate(user, remoteIssueLink);
if (remoteIssueLinkService.create(user, createValidationResult) == null) {
// set custom field 'Add TCE document' to "" now the link has been created
TCEDoc1.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(TCEDoc1), ""),new DefaultIssueChangeHolder());
return "Link not created";

} else {
// set custom field 'Add TCE document' to "" now the link creation has failed
TCEDoc1.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(TCEDoc1), ""),new DefaultIssueChangeHolder());
return "Ok";
}
} else {
// set custom field 'Add TCE document' to "" the link already exists 
TCEDoc1.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(TCEDoc1), ""),new DefaultIssueChangeHolder());
// set custom field 'TCE document type' to "" the link already exists 
TCEDoc2.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(TCEDoc2), ""),new DefaultIssueChangeHolder());
return "NOK"
}
0 votes
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.
August 28, 2014

I'd do this with a derived field (I usually do those with the script runner addon)

If you give the user a text field for them to enter the number on create/edit screens, but then put a derived field in that displays "<basic url>/number", then you're done. I'd probably display it as a full link as well, and this has the advantage that if you move the CMS, you only need to tweak the script to update all your issues.

Rudy Holtkamp August 28, 2014

@NicThanks for your reply. But that is a bit rudimentary, since there is an excellent out-of-the-box screen which contains (currently) two options. I would think it should be possible to put in another. After all the 'Jira issue' option is specific kind of web link.

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.
August 28, 2014

Solves the problem with minimal complexity.

You could do something more advanced of course. Your users could simply use the links too, but it sounds like you want to be a bit more clever.

You could also take the same starting point as I suggested - a simple field with just the number to be entered. Then write a listener that converts it into a web-link when people update the issue...

Suggest an answer

Log in or Sign up to answer