Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy script thinks cfValues is a variable

Maya_Chase May 15, 2018

Hi, I'm trying to cobble together a validator script to check whether a checkbox field called New Hire Account Provisioning contains the value Egencia, at the same time checking to see if either of two custom fields is empty. If Egencia is checked and either or both of the two custom fields is empty, I want to throw up an error message. I've done this kind of thing before, and patterned this script off of the one that worked, but this one has the message "[Static type checking] - The variable [cfValues] is undeclared" on my if statement. Obviously cfValues isn't a variable, and I can't see why I'm getting this message. I'm sure I'm missing something obvious, so if you can see it, please help!

My script:

import com.atlassian.jira.issue.Issue
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category

def issueManager = ComponentAccessor.getIssueManager()
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def cField = customFieldManager.getCustomFieldObject('customfield_15564')
log.error (cField)
def cFieldValue = issue.getCustomFieldValue(cField)
log.error (cFieldValue)

def cField2 = customFieldManager.getCustomFieldObject('customfield_16662')
log.error (cField2)
def cFieldValue2 = issue.getCustomFieldValue(cField2)
log.error (cFieldValue2)

if ((cfValues['New Hire Account provisioning']*.value.contains('Egencia')) && ((cFieldValue == null) || (cFieldValue2 == null)))
{
invalidInputException = new InvalidInputException("Manager Email", "Both Department Number and Manager Email are required when specifying an Egencia account be set up.")
}

1 answer

0 votes
Praveen
Contributor
May 15, 2018

Hi Maya,

Probably you should use the other way of getting the custom field value? Refer the link below,

https://community.atlassian.com/t5/Jira-questions/How-to-validate-all-checkboxes-are-checked-using-groovy/qaq-p/154524

-Praveen

Maya_Chase May 17, 2018

Hi Praveen, thanks for answering! I've modified the script to:

import com.atlassian.jira.issue.Issue
import com.opensymphony.workflow.InvalidInputException
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Category

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def optionsManager = ComponentAccessor.getOptionsManager()
def fieldConfigSchemeManager = ComponentAccessor.getFieldConfigSchemeManager()

def cf = customFieldManager.getCustomFieldObjectByName("New Hire Account provisioning")
def config = fieldConfigSchemeManager.getRelevantConfig(issue, cf)
def allOptions = optionsManager.getOptions(config)
def checkboxFieldValues = cf.getValue(issue)

def cField = customFieldManager.getCustomFieldObject('customfield_15564')
def cFieldValue = issue.getCustomFieldValue(cField)

def cField2 = customFieldManager.getCustomFieldObject('customfield_16662')
def cFieldValue2 = issue.getCustomFieldValue(cField2)

checkboxFieldValues.each { optionValue ->
if (optionValue == 'Egencia')
log.error('Egencia found')
}

-----------

It all passes the syntax test, and I've confirmed that optionValue does actually equal Egencia when that option is checked, with a logging statement, but the if statement never evaluates to true. Any idea why?

Maya_Chase May 17, 2018

Ok, I figured out the if statement, it needed to be cast to a string. So that part is now:

checkboxFieldValues.each { optionValue ->
if ((optionValue.toString() == "Egencia") && ((cFieldValue == null) || (cFieldValue2 == null)))
invalidInputException = new InvalidInputException("Manager Email", "Both Department Number and Manager Email are required when specifying an Egencia account be set up.")
}

But now I'm back to my original question - it thinks invalidInputException is a variable. What gives?

Praveen
Contributor
May 17, 2018

Hi Maya,

Can you try the below code, check the below links,

InvalidInputException e= new InvalidInputException()
e.addError("error message")
throw e;

 https://community.atlassian.com/t5/Jira-questions/The-simplest-Groovy/qaq-p/350307

https://community.atlassian.com/t5/Answers-Developer-Questions/How-to-pass-variable-in-error-message-in-script-runner/qaq-p/493837

-Praveen

Maya_Chase May 18, 2018

Now it says the variable e is undeclared. I don't see anything in the links where e has to be declared?

Nic Brough -Adaptavist-
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 18, 2018

Spaces matter in code - "e=" is not the same as "e =".  I think Praveen missed a space in the first line of code in the last comment.

Maya_Chase May 21, 2018

Hi Nic, thanks for that, I added the space between e and = but it is still telling me that e is an undeclared variable on the e.addError("error message") line. 

Suggest an answer

Log in or Sign up to answer