Validation of "Select List" based on number fields.

Hello, experts!

I faced with the problem of validation "select list field" via behavior plugin. 
If I filled number fields then update the validation doesn't work properly, if try to update it once again that validation is working.

Here is my Code:

def CoP = getFieldByName("Currency of payments to partners")
def NC = getFieldByName("NC mark-up to Billable costs (if applicable)")
def OPNB = getFieldByName("Other payments to partners (Non-Billable)")
def HPNB = getFieldByName("Hosting payments to partners (Non-Billable)")
def HWNB = getFieldByName("HW payments to partners (Non-Billable)")
def PSNB = getFieldByName("PS payments to partners (Non-Billable)")
def SMNB = getFieldByName("S&M payments to partners (Non-Billable)")
def LPNB = getFieldByName("License payments to partners (Non-Billable)")
def OPB = getFieldByName("Other payments to partners (Billable)")
def HPB = getFieldByName("Hosting payments to partners (Billable)")
def HWB = getFieldByName("HW payments to partners (Billable)")
def PSB = getFieldByName("PS payments to partners (Billable)")
def SMB = getFieldByName("S&M payments to partners (Billable)")
def LPB = getFieldByName("License payments to partners (Billable)")
def TPP = getFieldByName("3rd party products")
String NCVal = NC.getValue()
String OPNBVal = OPNB.getValue()
String HPNBVal = HPNB.getValue()
String HWNBVal = HWNB.getValue()
String PSNBVal = PSNB.getValue()
String SMNBVal = SMNB.getValue()
String LPNBVal = LPNB.getValue()
String OPBVal = OPB.getValue()
String HPBVal = HPB.getValue()
String HWBVal = HWB.getValue()
String PSBVal = PSB.getValue()
String SMBVal = SMB.getValue()
String LPBVal = LPB.getValue()
String TPPVal = TPP.getValue()
if ((NCVal != "" ||) (OPNBVal != "") || (HPNBVal != "") || (HWNBVal != "") || (PSNBVal != "") || (SMNBVal != "") || (LPNBVal != "") || (OPBVal != "") || (HPBVal != "") || (HWBVal != "") || (PSBVal != "") || (SMBVal != "") || (LPBVal != "") || (TPPVal != "")) {
    CoP.setRequired(true)
}else{
    CoP.setRequired(false)
}

Perhaps the "None" value is the reason of this issue.
Hope you help! 

2 answers

0 vote
Vasiliy Zverev Community Champion Nov 24, 2016

At first I would recommend you to refactor code like this. Current version is too hard to find bugs.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.fields.CustomField


def CoP = getFieldByName("Currency of payments to partners")
CoP.setRequired(true);

List<String> customFieldsNames = Arrays.asList(
         "NC mark-up to Billable costs (if applicable)"
        , "Other payments to partners (Non-Billable)"
        , "Hosting payments to partners (Non-Billable)"
        , "HW payments to partners (Non-Billable)"
        , "PS payments to partners (Non-Billable)"
        , "S&M payments to partners (Non-Billable)"
        , "License payments to partners (Non-Billable)"
        , "Other payments to partners (Billable)"
        , "Hosting payments to partners (Billable)"
        , "HW payments to partners (Billable)"
        , "PS payments to partners (Billable)"
        , "S&M payments to partners (Billable)"
        , "License payments to partners (Billable)"
        , "3rd party products"
)

CustomField customField;
CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

for(String fieldName: customFieldsNames){
    customField = customFieldManager.getCustomFieldObjectByName(fieldName)
    if( issue?.getCustomFieldValue(customField).equals("") ){
            CoP.setRequired(false);
        break;
    }
}
Vasiliy Zverev Community Champion Nov 24, 2016

This is for groovy script, I am not sure is it suitable for behavior.

@Vasiliy Zverev thank you for the answer!
Your script doesn't work with behavior feature. 

Vasiliy Zverev Community Champion Nov 24, 2016

Are there any errors?

@Vasiliy Zverev greetings!

Code did not work out with behavior feature. 
Solved via workflow validator (simple script validator).

if ((cfValues["NC mark-up to Billable costs (if applicable)"] != null) || (cfValues["License payments to partners (Billable)"] != null) || (cfValues["Other payments to partners (Billable)"] != null) || (cfValues["License payments to partners (Non-Billable)"] != null) || (cfValues["S&M payments to partners (Non-Billable)"] != null) || (cfValues["PS payments to partners (Non-Billable)"] != null) || (cfValues["HW payments to partners (Non-Billable)"] != null) || (cfValues["Hosting payments to partners (Non-Billable)"] != null) || (cfValues["Other payments to partners (Non-Billable)"] != null) || (cfValues["S&M payments to partners (Billable)"] != null) || (cfValues["PS payments to partners (Billable)"] != null) || (cfValues["HW payments to partners (Billable)"] != null) || (cfValues["Hosting payments to partners (Billable)"] != null))  {
    if (! cfValues["Currency of payments to partners"]) {
        return false
    }
}
true

A validator will work for workflow steps, but that will only apply to changes that take place during a workflow transaction. If you want to guide edits to the issue as well, you will need to use a behaviour.

If Vasiliy's code isn't working, I'd recommend you try extending the FieldBehaviours class using the BaseScript annotation. In fact, you'll need to do this if the Behaviour script is being read from a Script File, instead of as an inline script. Something like...

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours fieldBehaviours

def CoP = getFieldByName("Currency of payments to partners")
List<String> customFieldsNames = ["NC mark-up to Billable costs (if applicable)",
                                  "Other payments to partners (Non-Billable)",
                                  "Hosting payments to partners (Non-Billable)",
                                  "HW payments to partners (Non-Billable)",
                                  "PS payments to partners (Non-Billable)",
                                  "S&M payments to partners (Non-Billable)",
                                  "License payments to partners (Non-Billable)",
                                  "Other payments to partners (Billable)",
                                  "Hosting payments to partners (Billable)",
                                  "HW payments to partners (Billable)",
                                  "PS payments to partners (Billable)",
                                  "S&M payments to partners (Billable)",
                                  "License payments to partners (Billable)",
                                  "3rd party products"]

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

boolean anyFieldsEmpty = customFieldsNames.any{ fieldName ->
    CustomField customField = customFieldManager.getCustomFieldObjectByName(fieldName)
    def value = issue?.getCustomFieldValue(customField)
    value == null || value == ""
}
log.warn("Were any of the field empty: $anyFieldsEmpty")

CoP.setRequired(!anyFieldsEmpty)

You'll also notice I added a log.warn statement. log.debug might be more appropriate, but then you'd need to turn up the logging level to debug for ScriptRunner.

That at least should give you a start at figuring out what went wrong with the behaviour. Of course, if the workflow validator is enough for your purposes, and you don't need this rule to be enforced except on workflow steps, that's fine. It just may be a bit opaque to a user why things aren't working. Also, it may prove easier to debug if you simplify the script the same way Vasiliy did your original...

List<String> customFieldsNames = ["NC mark-up to Billable costs (if applicable)",
                                  "Other payments to partners (Non-Billable)",
                                  "Hosting payments to partners (Non-Billable)",
                                  "HW payments to partners (Non-Billable)",
                                  "PS payments to partners (Non-Billable)",
                                  "S&M payments to partners (Non-Billable)",
                                  "License payments to partners (Non-Billable)",
                                  "Other payments to partners (Billable)",
                                  "Hosting payments to partners (Billable)",
                                  "HW payments to partners (Billable)",
                                  "PS payments to partners (Billable)",
                                  "S&M payments to partners (Billable)",
                                  "License payments to partners (Billable)",
                                  "3rd party products"]
boolean anyRelevantFieldsPopulated = customFieldsNames.any{ fieldName ->
    cfValues[fieldName] != null && cfValues[fieldName] != ""
}
return !(anyRelevantFieldsPopulated && !cfValues["Currency of payments to partners"])

Hi Sergey,

what kind of custom fields are these ? 

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

Meet the AUG leaders of Northern Virginia

@Rachel Wright (Jira Genie), @Billy Poggi (AUG NOVA, DC), and @Dana Jansen (Confluence Queen) are just some of the folks that lead one of the world's most active Atlassian User Group (AUG)....

121 views 4 9
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