Scriptrunner Validator for cascading field to only apply to certain issue types on transition

Stephen Letch January 16, 2019

Hi

 

I've seen a bunch of examples with slightly different requirements. But my requirement is on a transition that 5 different issue types can go down, to only check two of them for BOTH levels of a cascading field to be filled in.

 

For example:

If Issue type = Service Request

then

Both levels of 'Service Request Type' cascading field need to be filled in

 

if Issue Type = Incident

then

Both levels of 'Incident Type' cascading field need to be filled in.

 

and to not worry about the other 3 issue types.

 

I managed to fish out this example from Googling around for the cascading field requirements, but I need to know how to how to make it only check depending on the issue type

 

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

def issue = issue as MutableIssue
def cascadingSelect = ComponentAccessor.customFieldManager.getCustomFieldObjects(issue)?.findByName("Service Request Type")
def values = issue.getCustomFieldValue(cascadingSelect) as HashMap

// Will return true if both the parent and child options in the Cascading Select have a value
values?.keySet()?.size() == 2 

 

2 answers

0 votes
Stephen Letch January 17, 2019

Hi

 

After a bunch of testing, this seems to only be paying attention to the first if statement, especially if I try and split it out to get two different error messages in the simple scripted validator.

 

It doesn't seem to be checking for your invalid input message

0 votes
Tansu Akdeniz
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 16, 2019

Hi @Stephen Letch,

The following custom script validator may guide you;

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField
import com.opensymphony.workflow.InvalidInputException

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

CustomField incidentTypeCF = customFieldManager.getCustomFieldObject("customfield_XXXXX");
if(issue.getIssueTypeId().equals("12345")){ // Incident Issue Type ID
if(issue.getCustomFieldValue(incidentTypeCF)?.values()*.value[0] == null
|| issue.getCustomFieldValue(incidentTypeCF)?.values()*.value[1] == null){
invalidInputException = new InvalidInputException("Please fill both cascading values..")
}
}

CustomField serviceRequestTypeCF = customFieldManager.getCustomFieldObject("customfield_XXXXX");
if(issue.getIssueTypeId().equals("54321")){ // Service Request Issue Type ID
if(issue.getCustomFieldValue(serviceRequestTypeCF)?.values()*.value[0] == null
|| issue.getCustomFieldValue(serviceRequestTypeCF)?.values()*.value[1] == null){
invalidInputException = new InvalidInputException("Please fill both cascading values..")
}
}
Stephen Letch January 16, 2019

Getting errors on creation and also doesnt seem to stop the transition

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField
import com.opensymphony.workflow.InvalidInputException

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

CustomField incidentTypeCF = customFieldManager.getCustomFieldObject("customfield_11428");
if(issue.getIssueTypeId().equals("10103")){ // Incident Issue Type ID
 if(issue.getCustomFieldValue(incidentTypeCF)?.values()*.value[0] == null
 || issue.getCustomFieldValue(incidentTypeCF)?.values()*.value[1] == null){
 invalidInputException = new InvalidInputException("Please fill both cascading values..")
 }
}

CustomField serviceRequestTypeCF = customFieldManager.getCustomFieldObject("customfield_11417");
if(issue.getIssueTypeId().equals("10104")){ // Service Request Issue Type ID
 if(issue.getCustomFieldValue(serviceRequestTypeCF)?.values()*.value[0] == null
 || issue.getCustomFieldValue(serviceRequestTypeCF)?.values()*.value[1] == null){
 invalidInputException = new InvalidInputException("Please fill both cascading values..")
 }
}

 

SC error.png

 

EEDIT: It worked when I changed

ccustomfield_11428 to Incident

 

EDIT 2: It doesn't seem to be paying attention to the invalidinput area and also if it s a simple scripted one it requires you to fill in the error field below anyway. If you use custom script so that it might use the invalidinput message then none of it blocks anything.

 

Think I'm going to split it up into two validators so it can show the separate messages depending on context, thanks very much though, this has helped a lot.

 

Suggest an answer

Log in or Sign up to answer