How to set "Linked Issues" as mandatory field on issue creation?

Samuel Titka April 2, 2015

Hi there!

My goal is to force users to link the newly created issue with the existing one (parent - child hierarchy). To ensure this, I set the "Linked Issues" field as required in the Field configuration. But this is obviously not enough because it doesn't work as expected.

Let me explain:

  • First line - The relation type is chosen ("is Child of" is selected as default)
  • Second line - Related issue is not selected - this field stays empty

parent-child.png

Despite the fact, that the second line is empty, the issue will be created - as a result, no issue link is generated. This is undesirable behavior. Only the relation type filling is checked. JIRA unfortunately does not check if the second line is filled in.

Do you guys have any ideas how to achieve the desired functionality? How to force the users to fill in also the related issue - the second line?

Thanks for your time!

5 answers

1 accepted

2 votes
Answer accepted
randyz May 5, 2015

I'm on a similar quest, but I only have half the answer for my goal.  However this may help you with some tweaking or no tweaking.

I am on a quest to fix users that would not link issue when resolving issues as duplicate.

I found the following snippet code that will force users to fill in the "empty" space in issue links

request = webwork.action.ActionContext.getRequest()

if (request){
// check for new duplicates link
linktype = request.getParameter('issuelinks-linktype')
linkedIssue = request.getParameter('issuelinks-issues') 
}
Samuel Titka May 10, 2015

Thank you Randy. This is exactly what I was looking for!

Karanpreet Kaur April 26, 2019

Hello,

Can you please provide more information on where to use this snippet. I am running into same issue.

Thanks,
Karan

2 votes
Samuel Titka July 29, 2015

Hi there,

sooo I created this script. It should provide the functionality.

You can also set which link type is required and which issue types are allowed.

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;

String paramRequiredLinkType = "relates to";
List paramRequiredIssueTypes = Arrays.asList("Bug", "New Feature");

def request = webwork.action.ActionContext.getRequest();
if (request) {
    String linkType = request.getParameter('issuelinks-linktype');
    String[] linkedIssues = request.getParameterValues('issuelinks-issues');
    if (linkType != null && linkType.equals(paramRequiredLinkType) && linkedIssues != null) {
        if (linkedIssues.size() > 0) {
            IssueManager myIssueManager = ComponentManager.getInstance().getIssueManager();
            for (String tmpLinkedIssue: linkedIssues) {
                MutableIssue toBeLinkedIssue = myIssueManager.getIssueObject(tmpLinkedIssue);
                if (toBeLinkedIssue != null) {
                    if (!paramRequiredIssueTypes.contains(toBeLinkedIssue.issueTypeObject.name)) {
                        return false;
                    }
                } else {
                    return false;
                }
            }
            return true;
        }
    }
}
return false;
Sri Kanth November 23, 2015

Super. Thanks!

Raghav R January 23, 2018

Hi, I have the same requirement.
however, the line
IssueManager myIssueManager = ComponentManager.getInstance().getIssueManager();
now doesn't work on the newest version of JIRA stating that it is deprecated and method getIssueManager cannot be found in ComponentManager. Please advise what is the alternative code that can be used?

Samuel Titka January 24, 2018

Hi, should use the ComponentAccessor instead the ComponentManager. It means that you should replace the following lines:

import com.atlassian.jira.ComponentManager;
IssueManager myIssueManager = ComponentManager.getInstance().getIssueManager();

with these lines:

import com.atlassian.jira.component.ComponentAccessor;
def myIssueManager = ComponentAccessor.getIssueManager();

Cheers,
Samuel

Raghav R January 24, 2018

Thanks heaps to you Samuel.

The line in question works now.

However, I see warning with regards to 'IssueTypes' indicating deprecated function call for the line

if (!paramRequiredIssueTypes.contains(toBeLinkedIssue.issueTypeObject.name)) 

on the latest version of JIRA. Hence, the script is not executing.

Please advise how it could be changed to.

Samuel Titka January 25, 2018

Hi,

yes, sometimes with the JIRA updgrade some of the methods become obsolete/deprecated. In such cases it is necessary to go through the API documentation and find appropriate methods. Usually, there is a note that the method is deprecated and there is also a link to the replacement.

Regarding the line you mentioned, please try this:

if (!paramRequiredIssueTypes.contains(toBeLinkedIssue.getIssueType().getName())) {

Cheers,

Samuel

Raghav R January 29, 2018

Thank you Samuel.

I executed the full script. It runs well, but doesn't behave as expected in the title of this query. Please advise if you have a full working script?

The requirement is to create a Spike issue type, with mandatory 'relates to' to a 'Bug' or 'Epic'.

Need a Validator script for the same on latest JIRA.

Thank you

0 votes
Neirouz Garhe January 22, 2019

Here is a full version of a working script:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;

String paramRequiredLinkType = "is child of";
List paramRequiredIssueTypes = Arrays.asList("Contract");

def request = webwork.action.ActionContext.getRequest();
if (request) {
String linkType = request.getParameter('issuelinks-linktype');
String[] linkedIssues = request.getParameterValues('issuelinks-issues');
if (linkType != null && linkType.equals(paramRequiredLinkType) && linkedIssues != null) {
if (linkedIssues.size() > 0) {
def myIssueManager = ComponentAccessor.getIssueManager();
for (String tmpLinkedIssue: linkedIssues) {
MutableIssue toBeLinkedIssue = myIssueManager.getIssueObject(tmpLinkedIssue);
if (toBeLinkedIssue != null) {
if (!paramRequiredIssueTypes.contains(toBeLinkedIssue.getIssueType().getName())) {
return false;
}
} else {
return false;
}
}
return true;
}
}
}
return false;
Karl Samson October 27, 2023

Hi Neirouz, thanks for this code. Very clever!

However, as much as it makes the 'Linked Issues and issues fields mandatory, it doesn't set the relationship, as suggested in the code, nor once populated, does it accept the input and allow the issue to be updated!?

Could you please suggest why the fields now don't accept an input?

Thanks,

Karl.

0 votes
Samuel Titka April 8, 2015

Anybody? Is this even possible to achieve?

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.
April 2, 2015

Links are not fields, they're relationships between issues, which makes it a bit harder than just flagging a field (in fact, the field you've tried to flag is ephemeral - it's populated by duplicating the link information into it when there are links, and the data you enter into it is used to create links, and then its discarded.  The field doesn't actually exist in the database)

The best you can do here is to add a validator to the create transition - one that checks that at least one link is defined.  It will not be able to highlight the links field as mandatory on the create screen, and it only kicks in when the user clicks "create" - a well written validator will return them back to the screen and have a good message to tell them what they've missed.

There are several validators in the marketplace (mostly as part of bigger addons), or, as ever, you can always do this with the script runner (maximum flexibility there, but you'll need to do a little bit of coding)

Samuel Titka April 2, 2015

Hello Nic, thanks for your reply. I tried to use Script runner but I was unsuccessful. Here's why: - I have added Custom script validator on CREATE transition - Snippet of condition in validator: return issueLinkManager.getOutwardLinks(issue.getId())*.issueLinkType.name.contains('Hierarchy') - I THINK that the condition is checking for already existing links. But on the Create the link hasn't been already created. So JIRA has nothing to return. I understand that this condition will work on subsequent transitions but not on the Create - please correct me if I am wrong. Do I have any other options?

Samuel Titka April 2, 2015

I have also tried "getInwardLinks(...)" with no result.

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.
April 7, 2015

A validator should return "true" for "passed validation" or "false" (and ideally, an exception message) if validation fails. The validator is passed the current issue object that the system is *going* to create, so the links should be available on it, but you'll need to check for both inward and outward links (and I'm not sure what the line of code you've posted is really trying to do)

Samuel Titka April 7, 2015

I tried to use (k) SIL Validator (JJupin Add-on) but with no result. Code: string[] linkedIssuesHierarchy = linkedIssues(key, "Hierarchy", 0); // checks both inwards and outwards links if (size(linkedIssuesHierarchy) > 0) { return true; } else { return false; } Result was only error message: Exception occurred: com.atlassian.jira.util.dbc.Assertions$NullArgumentException: issue should not be null! I think that JJupin issue "key" variable resolution does not work on Create transition. As far as I know, If a transition's validator FAILS, the transition's post functions will NOT be executed. On Create transition there are post-functions for issue creation. So I think issue does not exists (maybe only some kind of object that you have noted) in this step so clearly this issue cannot be linked to real existing JIRA issues.

Samuel Titka April 8, 2015

I have also tried the method hasInput of JJupin add-on (however it is documented only for custom fields) https://confluence.kepler-rominfo.com/display/SIL/hasInput Code: if (hasInput("issuelinks")) { return true, "issuelinks-issues", "test OK"; } else { return false, "issuelinks-issues", "test FALSE"; } But again, no result... this condition was always true no matter if I filled in also linked issues field or only the link type. I tried to replace "issuelinks" with "issuelinks-issues" but this condition was always false.

Suggest an answer

Log in or Sign up to answer