With Adaptavist Scriptrunner or MyGroovy plugins for Jira Server and a little groovy script we can validate/require or make mandatory the second drop down of a cascading field.

Just create a Simple scripted validator in the transition! Example:


cfValues["The name of your customfield"]?.keySet()?.size() == 2

Very easy!

Example:

captura de pantalla 2019-01-29 a las 17.37.32

By MrAddon

Posted by:.

10 replies on “Solution for making second drop down required in cascading custom field

  1. MyGroovy:
    Maybe I complicate it, but I not found easier solution than this:
    import com.opensymphony.workflow.InvalidInputException
    import com.atlassian.jira.component.ComponentAccessor
    if (issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObject(“customfield_XXXX”))?.keySet()?.size() == 2) {
    return true
    } else {
    throw new InvalidInputException(“error message”)
    }

    Liked by 1 person

    1. Hello Nihat,

      Here an example of validation with Behaviours

      //Required Imports
      import groovy.time.TimeCategory
      import java.util.Date
      
      // Get a pointer to my Date Custom field
      def DateField = getFieldByName("DemoTimeField")
      Date DateVal = (Date) DateField.getValue()
      
      // Get todays date
      def timeIn1Hour = new Date()
      
      // Add 1 hour to todays date
      use( TimeCategory ) {
          after60Mins = timeIn1Hour + 60.minutes
      }
      
      // Check if the Start Date is before todays date plus 1 hour and if so present the user with an error and clear the value to the field to force the user to enter in a correct value.
      if(DateVal.before(after60Mins)){
          DateField.setError("You are only allowed to select a Date and time that is greater than 1 hour from now" + "<br/>" + "Your selected date and time has been cleared to prompt you to select a valid value")
          // Clear the weekday value to make the user re select a new value as well
          DateField.setFormValue(null)
      }else{
          // If a valid value is entered clear any errors and accept the value
          DateField.clearError()
      }
      

      Like

      1. Hi MrAddon,
        I think, I couldn’t explain. But, very important to your examle for me.
        I have a custom field (cascading select list). If creating new issue, I want to required second field (child) on cascading filed
        Thanks

        Like

      2. You can use simple script validators or more complex script validators

        Example:

        //...
         if (cfValues['cascade_field']?.values()*.value == ['AA', 'AB']) { }
        //..
        cfValues['your cascading field']?.values()*.value.size() == 2
        //...
        // More complex...
        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..")
         }
        }
        
        

        Regards

        Like

  2. You can use simple script validators or more complex script validators

    Example:

    //...
     if (cfValues['cascade_field']?.values()*.value == ['AA', 'AB']) { }
    //..
    cfValues['your cascading field']?.values()*.value.size() == 2
    //...
    // More complex...
    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..")
     }
    }
    
    

    Regards

    Like

  3. Hi MrAddon,
    There are two issue type. “ABC” issue type using “cascading1” custom field and “XYZ” issue type using “cascading2” custom field. I want to make mandatory custom field according the issue type.
    Thanks.

    Like

    1. Easy! Is a simple script behaviour with ScriptRunner

      if (getIssueContext().getIssueType().getName().equals("ABC")) {
      def desc = getFieldByName("cascading1")
      desc.setRequired(true)
      }
      
      if (getIssueContext().getIssueType().getName().equals("XYZ")) {
      def desc = getFieldByName("cascading2")
      desc.setRequired(true)
      }
      

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s