Help with jira validation based on certain custom field values

Ganga Bopaiah July 20, 2016

I am a starter with scripting. I need to add a validation at a transition where if the resolution is "Fixed" and a field called "Defect Flags" (multi-select Check box) has "regression" chosen as one of the value, then another field called "Regression Introduced By" (Cascading field) should not be empty and a validation error message needs to be thrown.

 

I tried adding this script, any help on this?

import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.util.IssueChangeHolder
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.MutableIssue
import java.text.SimpleDateFormat
import java.util.*
import java.sql.Timestamp

ComponentManager componentManager = ComponentManager.getInstance()
//CustomFieldManager customFieldManager = componentManager.getCustomFieldManager()
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
//issue = componentManager.getIssueManager().getIssueObject("CAN-9829")
CustomField resolution = customFieldManager.getCustomFieldObjectByName("Resolution");
Object resolutionValue = resolution.getValue(issue);
CustomField DefectFlag = customFieldManager.getCustomFieldObjectByName("Defect Flags");
Object DefectFlagValue = DefectFlag.getValue(issue);
CustomField RegressionIntroducedBy = customFieldManager.getCustomFieldObjectByName("Regression Introduced By");
Object RegressionIntroducedByValue = RegressionIntroducedBy.getValue(issue);

if ((resolutionValue == "Fixed") && (DefectFlagValue.equals("Regression")) && (RegressionIntroducedByValue.equals("None")) )

{ error message here }

1 answer

2 votes
adammarkham
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.
July 21, 2016

You can add this as a custom script validator with the following script:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.customfields.option.Option
import com.opensymphony.workflow.InvalidInputException

def issue = issue as Issue

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def detectFlagsField = customFieldManager.getCustomFieldObjectByName("Defect Flags")
def introducedByField = customFieldManager.getCustomFieldObjectByName("Regression Introduced By")

def detectFlagsValue = issue.getCustomFieldValue(detectFlagsField) as List<Option>

def introducedByValue = issue.getCustomFieldValue(introducedByField)

if (issue.resolution?.name == "Done" && detectFlagsValue*.value?.contains("Regression") && ! introducedByValue) {
    throw new InvalidInputException(introducedByField.id, "Must have introduced by!")
}

First we get the custom fields then get the values for the custom fields and the check each one along with the resolution. The cascading select will be null if none is selected and introduced by may contain a list of options so will check it contains "Regression".

Finally if all conditions pass then we throw an InvalidInputException to prevent the user submitting the form and it will show an error message next to the "Regression Introduced By" field.

Hope this helps,

Adam

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events