Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

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

Create Link to Existing Issue from Custom Field

When we submit certain Issue Types (document type, for instance), we have a custom field named, "Related Tech Review" that references the issue where the document was reviewed.

As a post function, I'd like to take the value that's entered in the "Related Tech Review" field and create a link to it. (Link type "Related TR").

I've read postings that discuss creating an issue and linking to it as a post function, but all I want is to grab the value from the custom field and create a link to an existing issue. Ideally, a message would pop up if the "Related Tech Review" field was null or the link couldn't be created for some reason.

1 answer

1 accepted

0 votes
Answer accepted
Tansu Akdeniz Community Leader Jun 23, 2021

Hi @Chris Annal 

Custom script is needed for it.

Please check this code for link creation from customfield which may help you. If you need to validate Related Tech Review in transition then you need to write validation script.

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.user.ApplicationUser;

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

Issue sourceIssue = ComponentAccessor.getIssueManager().getIssueObject("ABC-XXX");

CustomField techRevCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_11111");
String techRevVal = sourceIssue.getCustomFieldValue(techRevCF);

Issue destIssue = ComponentAccessor.getIssueManager().getIssueObject(techRevVal);

ComponentAccessor.getIssueLinkManager().createIssueLink(sourceIssue.getId(),destIssue.getId(),10001L,0L,currentUser)

 Java api: https://docs.atlassian.com/software/jira/docs/api/latest/

Thanks, Tansu!! This is very helpful...but I have a question. In your example, it looks like you are hard-coding the current issue number (

getIssueObject("ABC-XXX"); 

Is there a way to get that issue number as the current issue?

Tansu Akdeniz Community Leader Jun 23, 2021

You are welcome.

In script console, you can test with the code above via manuel issue key.

But in workflow, please use this one.

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.user.ApplicationUser;

ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

CustomField techRevCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_11111");
String techRevVal = issue.getCustomFieldValue(techRevCF);

Issue destIssue = ComponentAccessor.getIssueManager().getIssueObject(techRevVal);

ComponentAccessor.getIssueLinkManager().createIssueLink(issue.getId(),destIssue.getId(),10001L,0L,currentUser)

ps: I removed this line:

Issue issue = ComponentAccessor.getIssueManager().getIssueObject("ABC-XXX");

System will get "issue" as a current issue object automatically. So, you don't have to define it.

Like Chris Annal likes this

Thanks again, Tansu!!

I ran into a minor glitch in that the "current issue" has to exist (issue.getId()), so this post function might be placed after the initial "Create" step in a workflow. I also named the specific Link Type (issueLinkName) that I wanted to use, and so far, this seems to work. I actually put the custom field in a workflow screen that pops up during the transition, so the user is presented with it and can enter the desired issue number to link to. Another option might be to put it on an "Edit" screen, but not the "Create" screen. Anyway, this is what seems to work for me so far. Thanks again for all your help!!!

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.link.IssueLinkTypeManager
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.user.ApplicationUser;

// "customfield_xxxxx" is the custom field used for linking, replace with actual custom field ID
CustomField techRevCF= ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_xxxxx");
String techRevVal = issue.getCustomFieldValue(techRevCF);
Issue destIssue = ComponentAccessor.getIssueManager().getIssueObject(techRevVal);
final String destinationIssueKey = techRevVal

// the name of the issue link
final String issueLinkName = "Related_ETR"

// the sequence of the link
final Long sequence = 1L

// get current user to perform the linking
ApplicationUser currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();

def issueLinkTypeManager = ComponentAccessor.getComponent(IssueLinkTypeManager)
def issueManager = ComponentAccessor.issueManager
def destinationIssue = issueManager.getIssueByCurrentKey(destinationIssueKey)
assert destinationIssue: "ETR issue does not exist"
def availableIssueLinkTypes = issueLinkTypeManager.issueLinkTypes
def linkType = availableIssueLinkTypes.findByName(issueLinkName)
assert linkType : "Could not find link type with name $issueLinkName. Available issue link types are ${availableIssueLinkTypes*.name.join(", ")}"
ComponentAccessor.issueLinkManager.createIssueLink(issue.getId(),destinationIssue.id, linkType.id, sequence, currentUser)
Tansu Akdeniz Community Leader Jun 25, 2021

Glad to hear that it worked !! If you could accept the answer, it may help other people to find the answer easier.

Thanks again for all your help. I accepted the answer (and liked it, too). 

:)

For bonus points, someone may want to work on handling the error that is displayed if the target issue of the link is "null" (bad data entry). You touched on the need for validation, so that's what I'm working on now - but the script works fine when the target issue is an existing issue key value. 

:)

Tansu Akdeniz Community Leader Jun 25, 2021

Validator is easier than post-function. Sample one:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.opensymphony.workflow.InvalidInputException

CustomField techRevCF = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_11111");
String techRevVal = transientVars["issue"].getCustomFieldValue(techRevCF);

if(ComponentAccessor.getIssueManager().getIssueObject(techRevVal) == null){
invalidInputException = new InvalidInputException("Issue not found in Jira.")
}

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Jira

⏰ Day in the life of a Jira Admin!

Hello Community! We thoroughly enjoyed this just-for-fun conversation in the Jira Admin Group about what it's like to be a Jira Admin. For #JiraJuly, our talented designers created these graphics t...

931 views 2 22
Read article

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