Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Multiple nested if/else statements script failing halfway down

bSte November 29, 2018

I am trying to get the script below to work - I have tried adding it to each field in behaviours as well as on one field. The issue appears once I add the additional if statement. I have also tried this with an && statement which also breaks.

 

Thanks so much for your help!

 

import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.config.FieldConfigImpl

import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def allowedOptions = null
def fieldArcOptions = null
def fieldPlatformType = getFieldByName("Platform Type")
def valuePlatformType = fieldPlatformType.getValue().toString()
def fieldProductType = getFieldByName("Product Type (DI)")
def valueProductType = fieldProductType.getValue().toString()
def fieldClientType = getFieldByName("Client Type")
def valueClientType = fieldClientType.getValue().toString()
def fieldIntegration = getFieldByName("Integration")
def valueIntegration = fieldIntegration.getValue().toString()
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customFieldIntegration = customFieldManager.getCustomFieldObject(fieldIntegration.getFieldId())
def configIntegration = customFieldIntegration.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(configIntegration)
def issueManager = ComponentAccessor.getIssueManager()

if ((valuePlatformType.toString() == "[Bai]"))
if ((valueProductType.toString() == "Consult") || (valueProductType.toString() == "Standard") || (valueProductType.toString() == "Complex"))
if (valueClientType.toString() == "Provider") {
allowedOptions = fieldIntegration.setFieldOptions(options.findAll {
it.value in ["PPI", "Online", "Offline", "Dest", "Other"]});
}
else if ((valuePlatformType.toString() == "Bai"))
if ((valueProductType.toString() == "Consult") || (valueProductType.toString() == "Complex"))
if (valueClientType.toString() == "Buyer") {
allowedOptions = fieldIntegration.setFieldOptions(options.findAll {
it.value in ["Jrn", "S", "BPI", "Other"]});
}
else if ((valuePlatformType.toString() == "Bai"))
if ((valueProductType.toString() == "Ntar"))
if ((valueClientType.toString() == "Provider")) {
allowedOptions = fieldIntegration.setFieldOptions(options.findAll {
it.value in ["Offline", "Dest"]});
}
else if ((valuePlatformType.toString() == "Dgix") || (valuePlatformType.toString() == "Crosswise"))
if (valueProductType.toString() == "Standard")
if ((valueClientType.toString() == "Provider") || (valueClientType.toString() == "Buyer")) {
allowedOptions = fieldIntegration.setFieldOptions(options.findAll {
it.value in ["Registration", "Batch"]});
}
else if ((valuePlatformType.toString() == "Ais"))
if (valueProductType.toString() == "Standard")
if (valueClientType.toString() == "Provider") {
allowedOptions = fieldIntegration.setFieldOptions(options.findAll {
it.value in ["User Sync"]});
}
else if ((valuePlatformType.toString() == "Ais"))
if (valueProductType.toString() == "Standard")
if (valueClientType.toString() == "Buyer") {
allowedOptions = fieldIntegration.setFieldOptions(options.findAll {
it.value in ["Fose", "Roa"]});
}
else if ((valuePlatformType.toString() == "Eua") || (valuePlatformType.toString() == "Responsys"))
if (valuePlatformType.toString() == "Standard")
if ((valueClientType.toString() == "Buyer") || (valueClientType.toString() == "Provider")) {
allowedOptions = fieldIntegration.setFieldOptions(options.findAll {
it.value in ["Gool", "Private"]});
}

 

1 answer

Suggest an answer

Log in or Sign up to answer
0 votes
Ken McClean
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.
May 4, 2023

The first check against "[Bai]" included the brackets, which might have caused a check to fail. No example output or error messages were included, so it's impossible to say for sure.

There was also at least one missing curly brace at the end of the loop.

I rewrote the script for the benefit not of the original poster, but for anyone who's ever had to look at this kind of business logic applied to a page.

First of all, I added some comments. This is generally good practice when you're trying to nest IF statements down seven or eight levels.

Second, I turned it from one big if/else statement into a series of individual If statements.  It's perhaps a tiny bit less efficient, but it also makes the code a lot easier to read because the individual criteria checks aren't tied into the previous ones.

Finally, I removed the repeated casts to string, and simply declared them once at the start of the script. This helped things look a lot cleaner.

Unfortunately the formatting capabilities of the Atlassian forums don't preserve the indentation that I applied.

 

import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.onresolve.jira.groovy.user.FieldBehaviours
import com.onresolve.jira.groovy.user.FormField
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.fields.config.FieldConfigImpl

import groovy.transform.BaseScript
@BaseScript FieldBehaviours fieldBehaviours
def allowedOptions = null
def fieldArcOptions = null
def fieldPlatformType = getFieldByName("Platform Type")
def valuePlatformType = fieldPlatformType.getValue().toString()
def fieldProductType = getFieldByName("Product Type (DI)")
def valueProductType = fieldProductType.getValue().toString()
def fieldClientType = getFieldByName("Client Type")
def valueClientType = fieldClientType.getValue().toString()
def fieldIntegration = getFieldByName("Integration")
def valueIntegration = fieldIntegration.getValue().toString()
def optionsManager = ComponentAccessor.getOptionsManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def customFieldIntegration = customFieldManager.getCustomFieldObject(fieldIntegration.getFieldId())
def configIntegration = customFieldIntegration.getRelevantConfig(getIssueContext())
def options = optionsManager.getOptions(configIntegration)
def issueManager = ComponentAccessor.getIssueManager()

def valPlatType = valuePlatformType.toString()
def valProdType = valueProductType.toString()
def valClientType = valueClientType.toString()

if (valPlatType == "Bai" && (valProdType.contains("Consult") || valProdType.contains("Standard") || valProdType.contains("Complex")) && valClientType == "Provider") {
//If the Value Platform Type is "Bai"
//AND the Value Product type is either "Consult" OR "Standard" or "Complex"
//AND the Value Client Type is "Provider"

allowedOptions = fieldIntegration.setFieldOptions(options.findAll {
it.value in ["PPI", "Online", "Offline", "Dest", "Other"]
//Set the allowed options accordingly
})
}



if (valPlatType == "Bai" && (valProdType == "Consult" || valProdType == "Complex") && valClientType == "Buyer") {
//If the Value Platform Type is "Bai"
//AND the Value Product type is either "Consult" OR "Complex"
//AND the Value Client Type is "Buyer"

allowedOptions = fieldIntegration.setFieldOptions(options.findAll {
it.value in ["Jrn", "S", "BPI", "Other"]
//Set the available options accordingly
})
}



if (valPlatType == "Bai" && valProdType == "Ntar" && valClientType == "Provider") {
//If the Value Platform Type is "Bai"
//AND the Value Product type is "Ntar"
//AND the Value Client Type is "Provider"

allowedOptions = fieldIntegration.setFieldOptions(options.findAll {
it.value in ["Offline", "Dest"]
//Set the available options accordingly
})
}



if ((valPlatType == "Dgix" || valPlatType == "Crosswise") && valProdType == "Standard" && (valClientType == "Provider" || valClientType == "Buyer")) {
//If the Value Platform Type is "Dgix" OR "Crosswise"
//AND the Value Product type is "Standard"
//AND the Value Client Type is "Provider" OR "Buyer"

allowedOptions = fieldIntegration.setFieldOptions(options.findAll {
it.value in ["Registration", "Batch"]
//Set the available options accordingly
})
}



if (valPlatType == "Ais" && valProdType == "Standard" && valClientType == "Provider") {
//If the Value Platform Type is "Ais"
//AND the Value Product type is "Standard"
//AND the Value Client Type is "Provider"

allowedOptions = fieldIntegration.setFieldOptions(options.findAll {
it.value in ["User Sync"]
//Set the available options accordingly
})
}



if (valPlatType == "Ais" && valProdType == "Standard" && valClientType == "Buyer") {
//If the Value Platform Type is "Ais"
//AND the Value Product type is "Standard"
//AND the Value Client Type is "Buyer"

allowedOptions = fieldIntegration.setFieldOptions(options.findAll {
it.value in ["Fose", "Roa"]
//Set the available options accordingly
});
}



if ((valPlatType == "Eua" || valPlatType == "Responsys") && valPlatType == "Standard" && (valClientType == "Buyer" || valClientType == "Provider")) {
//If the Value Platform Type is "Eua" OR Responsys
//AND the Value Product type is "Standard" OR "Buyer"
//AND the Value Client Type is "Provider"

allowedOptions = fieldIntegration.setFieldOptions(options.findAll {
it.value in ["Gool", "Private"]
//Set the available options accordingly
});
}



 

TAGS
AUG Leaders

Atlassian Community Events