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.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Glad to hear that it worked !! If you could accept the answer, it may help other people to find the answer easier.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
:)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.")
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.