Help with jira validation based on certain custom field values

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

This widget could not be displayed.

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
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

526 views 3 19
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you