We currently have many fields in our environment which support using Groovy scripts to perform this task. However, I need some help getting started with the script.
Essentially, I need to have a script that includes/removes values from a field (instead of replacing the value) based on whether other fields contain specific values.
This is the logical pseudo code example:
IF Custom Field A contains "value 1" AND Custom Field B contains "value 2" AND “value 3” is selected in Custom Field C AND Custom Field D does NOT contain "value 4" INCLUDE “value 4” in Custom Field D
ELSE IF Custom Field A contains "value 1" AND Custom Field B contains "value 2" AND Custom Field D does NOT contain "value 5" INCLUDE “value 5” in Custom Field D
The details of how you implement this are going to depend on the types of fields that you are using. From your pseudo-code, I am going to assume that all of the fields are select lists of some type
If my assumption is correct, the following should work for you
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.event.type.EventDispatchOption;
// Get the custom fields
def cfA = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Custom Field A")[0];
def cfB = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Custom Field B")[0];
def cfC = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Custom Field C")[0];
def cfD = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Custom Field D")[0];
def issue = ComponentAccessor.getIssueManager().getIssueObject("");
// Get the current value in the Custom Fields
List valueA = issue.getCustomFieldValue(cfA) as List;
List valueB = issue.getCustomFieldValue(cfB) as List;
List valueC = issue.getCustomFieldValue(cfC) as List;
List valueD = issue.getCustomFieldValue(cfD) as List;
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
if (valueA.any {it.getValue() == "value 1"}
&& valueB.any {it.getValue() == "value 2"}
&& valueC.any {it.getValue() == "value 3"}
&& ! valueD.any {it.getValue() == "value 4"}
) {
def fieldConfig = cfD.getRelevantConfig(issue);
def option4 = ComponentAccessor.getOptionsManager().getOptions(fieldConfig)?.find {it.getValue() == "value 4"};
if (option4 != null) {
valueD.add(option4);
issue.setCustomFieldValue(cfD, valueD);
ComponentAccessor.getIssueManager().updateIssue(
currentUser, issue, EventDispatchOption.DO_NOT_DISPATCH, true);
}
}
I haven't tested this code. It will give you syntax checking errors for the if statement - you can ignore those.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.