ScriptRunner - How To Conditionally Require Tickets From a Specific Project to be Linked

Ricky Wang Lin October 18, 2021

Requirement:

Custom Field has three values:

  1. A
  2. B
  3. C

When Custom Field value = A, require at least 1 ticket from Project ABC to be linked in the Linked Issue field

When Custom Field value = B, require at least 1 ticket from Project EFG to be linked in the Linked Issue field

When Custom Field value = C, require at least 1 ticket from Project ABC AND EFG to be linked in the Linked Issue field

Can anyone provide any insight on this?  Thank you

1 answer

0 votes
Payne
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
October 19, 2021

Here's one approach. You can create a validator of type Simple Scripted Validator [ScriptRunner] and create a script like below. I haven't tested it, so it may need a bit of tweaking, but it should be mostly what you need and get you headed in the right direction. Then just give it a meaningful error message for when the script returns a value of false.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

CustomField myField = customFieldManager.getCustomFieldObjectByName("My Field")
String myFieldValue = issue.getCustomFieldValue(myField)

def linkFound = false;
def links = issue.getOutwardIssueLinks();

links.each {
if (myFieldValue.equals("A") && it.getDestinationObject().getProjectObject().getName().equalsIgnoreCase("ABC")) {
linkFound = true;
}
if (myFieldValue.equals("B") && it.getDestinationObject().getProjectObject().getName().equalsIgnoreCase("EFG")) {
linkFound = true;
}
if (myFieldValue.equals("C") && it.getDestinationObject().getProjectObject().getName().equalsIgnoreCase("ABC AND EFG")) {
linkFound = true;
}
}

return linkFound;

Suggest an answer

Log in or Sign up to answer