Forums

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

Static type checking warning with log.Debug in scriptrunner listener script

Jeff Hayes
Contributor
March 19, 2021

Hi,

For some reason that I don't understand, I am getting a warning when running the following.  It says that I "Variable "log masks a binding variable of the same name on Line 11 where I define log.  Can someone tell me how/why I need to change that to something else?

For reference, this code is still not doing what I want it to do, hence the reason I am trying to insert debug statements to try to figure out what is wrong.

import com.onresolve.jira.groovy.user.FieldBehaviours
import com.atlassian.jira.event.issue.AbstractIssueEventListener
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import org.apache.log4j.Logger
import org.apache.log4j.Level

def log = Logger.getLogger("GenerateWeight")

// Set log level
log.setLevel(Level.DEBUG)

def issue = event.issue as Issue

def ValStatementFieldName = 'Value Statement Impact'
def VOIFieldName = 'VOI Score'

log.debug "ValStatementFieldName is $ValStatementFieldName"
log.debug "VOIFieldName is $VOIFieldName"


// Value for a multi-select field will always be a list even if "None" is selected
// def multiSelectFieldValue = multiSelectField.value as List
// Value for weighted VOI score based on which checkboxes in Value Statement Impact are checked by the user
def weight

def customFieldManager = ComponentAccessor.customFieldManager

def VOIField = customFieldManager.getCustomFieldObjects(issue).findByName(VOIFieldName)
assert VOIField : "Could not find custom field with name $VOIFieldName"

def ValStatementField = customFieldManager.getCustomFieldObjects(issue).findByName(ValStatementFieldName)
assert ValStatementField : "Could not find custom field with name $ValStatementFieldName"

// Value for a multi-select field will always be a list even if "None" is selected
def multiSelectFieldValue = ValStatementField

log.debug "multiSelectFieldValue is $multiSelectFieldValue"


// If value is null
if (multiSelectFieldValue == [null])
{
weight = 0
}
if (multiSelectFieldValue == ["Revenue"])
{
weight+= 4
}
if (multiSelectFieldValue == ["Client"])
{
weight+= 3
}
if (multiSelectFieldValue == ["Member"])
{
weight+= 2
}
if (multiSelectFieldValue == ["User"])
{
weight+= 1
}

log.debug "weight is $weight at end of script"

VOIField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(VOIField), weight), new DefaultIssueChangeHolder())

 

Thanks,

Jeff

 

4 answers

2 accepted

1 vote
Answer accepted
Jeff Hayes
Contributor
March 22, 2021

I've solved the issue.  It all boiled down to the definitions of my variables being of the wrong type.  The solution involved the following changes:

 

String multiSelectFieldValue = ValStatementField.getValue(issue)

 

was changed to:

 

String multiSelectFieldValue = ValStatementField.getValue(issue)

 

And 

def weight = 0

 

Was changed to

 

Double weight = 0

 

Thanks again for the helpful changes above in our discussion, Hana.  Also, this specific thread was helpful in figuring out it was the way the variable was declared that caused the issue: 

https://community.atlassian.com/t5/Adaptavist-questions/Groovy-expression-using-quot-contains-quot-comparing-with-a/qaq-p/903522

 

Thanks,

Jeff

1 vote
Answer accepted
Hana Kučerová
Community Champion
March 20, 2021

Hi @Jeff Hayes ,

about the binding variable - I believe there is default log variable "prepared" (along with other variables like event etc.). so this is only notification, you're overwriting it. You can fix it using another variable name like myLog...

def myLog = Logger.getLogger("GenerateWeight")

 

After the quick check of your code, I can see two main problems:

 

def VOIField = customFieldManager.getCustomFieldObjects(issue).findByName(VOIFieldName)

This is probably returning collection of custom fields (even though the collection has probably only one object) - same problem is with the second custom field

 

def multiSelectFieldValue = ValStatementField

if ValStatementField is a custom field object, you need to get it's value using 

ValStatementField.getValue(issue)
Jeff Hayes
Contributor
March 20, 2021
Thank you, Hana!  That fixed that portion.  The only thing that is still not working is the following:

// Value for a multi-select field will always be a list even if "None" is selected
def multiSelectFieldValue = ValStatementField.getValue(issue)

mylog.debug "multiSelectFieldValue is $multiSelectFieldValue"


// If value is null
if (multiSelectFieldValue == [null])
{
weight = 0
}
if (multiSelectFieldValue == ["Revenue"])
{
weight+= 4
}
if (multiSelectFieldValue == ["Client"])
{
weight+= 3
}
if (multiSelectFieldValue == ["Member"])
{
weight+= 2
}
if (multiSelectFieldValue == ["User"])
{
weight+= 1
}

mylog.debug "weight is $weight at end of script"

 

Are my IF statements not written correctly?  I am trying to sum up the total value for "weight"  based on which of the multiple checkboxes are checked in my custom field, multiSelectFieldValue, but for some reason "weight" is NULL.

My debug log statements are showing the following:

2021-03-20 12:37:01,089 DEBUG [GenerateWeight]: multiSelectFieldValue is [Revenue, Member] 

2021-03-20 12:37:01,090 DEBUG [GenerateWeight]: weight is null at end of script
Hana Kučerová
Community Champion
March 20, 2021

Please try something like:

def weight = 0
if (multiSelectFieldValue.contains('Revenue')) {
weight += 4
}
if (multiSelectFieldValue.contains('Client')) {
weight += 3
}
if (multiSelectFieldValue.contains('Member')) {
weight += 2
}
if (multiSelectFieldValue.contains('User')) {
weight += 1
Jeff Hayes
Contributor
March 21, 2021

Hana,

Thank you.  Unfortunately, I get errors in the Console view of this script for each of the 4 "if (multiSelectFieldValue.contains(<checkbox field name>)"stating:

[static type checking] - Cannot find matching method java.lang.Object#contains(lava.lang.String).  Please check if the declared type is correct and if the method exists.  

And my debug log says the following after the listener runs:

2021-03-20 23:04:23,105 DEBUG [GenerateWeight]: ValStatementFieldName is Value Statement Impact 
2021-03-20 23:04:23,105 DEBUG [GenerateWeight]: VOIFieldName is VOI Score
2021-03-20 23:04:23,107 DEBUG [GenerateWeight]: multiSelectFieldValue is [Revenue, Member]
2021-03-20 23:04:23,108 DEBUG [GenerateWeight]: weight is 0 at end of script
2021-03-20 23:04:23,112 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2021-03-20 23:04:23,112 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double
at com.atlassian.jira.issue.customfields.impl.NumberCFType.getDbValueFromObject(NumberCFType.java:45)
at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.createValue(AbstractSingleFieldType.java:143)
at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693)
at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:410)
at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396)
at com.atlassian.jira.issue.fields.OrderableField$updateValue$2.call(Unknown Source) at GenerateWeight.run(GenerateWeight.groovy:63)

It appears that this means the IF statements checking for my custom multiple checkbox variable is not working, as best I can tell, since "weight" is still 0 at the end of the script, even though I have "Revenue" and "Member" checked when I created the new issue.  

Also, can you point me to a resource to use to understand which methods are available to me based on whether I am running a normal script or a listener script?  The way that I get and set variables seems to be wildly different when I check your example scripts for both of those on the Adaptavist site and I've yet to find a resource that tells me what to use and when.  Thanks!

 

Jeff

0 votes
Hana Kučerová
Community Champion
March 21, 2021

Hi @Jeff Hayes ,

just to be 100% sure - your ValStatementField has "Checkboxes" type? And VOIField has "Number Field" type? Thank you.

Jeff Hayes
Contributor
March 21, 2021

Yes, that's correct!

 

Jeff

Jeff Hayes
Contributor
March 22, 2021

AS you can see, my variable "weight" is not being correctly set to "6" as it should be if Revenue and Member are both checked.  Likewise, no actual number is being stored in my custom number field, "VOIField".  It's still a blank field when I view it, so something is wrong with that line in the script, as well. 

 

Thanks,

Jeff

Jeff Hayes
Contributor
March 22, 2021

For reference, I placed the following debug log statement to see if the IF statement is evaluating to FALSE, instead of TRUE as it should have been, and this is verified:

 

def fieldContainsValue = multiSelectFieldValue.contains('Revenue')

mylog.debug "fieldContainsValue is $fieldContainsValue"

 

LOG OUTPUT:

2021-03-22 14:28:59,686 DEBUG [GenerateWeight]: multiSelectFieldValue is [Revenue, Member] 

2021-03-22 14:28:59,686 DEBUG [GenerateWeight]: fieldContainsValue is false
0 votes
Jeff Hayes
Contributor
March 21, 2021

Hana,

Thank you.  Unfortunately, I get errors in the Console view of this script for each of the 4 "if (multiSelectFieldValue.contains(<checkbox field name>)"stating:

[static type checking] - Cannot find matching method java.lang.Object#contains(lava.lang.String).  Please check if the declared type is correct and if the method exists.  

And my debug log says the following after the listener runs:

2021-03-20 23:04:23,105 DEBUG [GenerateWeight]: ValStatementFieldName is Value Statement Impact 
2021-03-20 23:04:23,105 DEBUG [GenerateWeight]: VOIFieldName is VOI Score
2021-03-20 23:04:23,107 DEBUG [GenerateWeight]: multiSelectFieldValue is [Revenue, Member]
2021-03-20 23:04:23,108 DEBUG [GenerateWeight]: weight is 0 at end of script
2021-03-20 23:04:23,112 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2021-03-20 23:04:23,112 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Double
at com.atlassian.jira.issue.customfields.impl.NumberCFType.getDbValueFromObject(NumberCFType.java:45)
at com.atlassian.jira.issue.customfields.impl.AbstractSingleFieldType.createValue(AbstractSingleFieldType.java:143)
at com.atlassian.jira.issue.fields.ImmutableCustomField.createValue(ImmutableCustomField.java:693)
at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:410)
at com.atlassian.jira.issue.fields.ImmutableCustomField.updateValue(ImmutableCustomField.java:396)
at com.atlassian.jira.issue.fields.OrderableField$updateValue$2.call(Unknown Source) at GenerateWeight.run(GenerateWeight.groovy:63)

It appears that this means the IF statements checking for my custom multiple checkbox variable is not working, as best I can tell, since "weight" is still 0 at the end of the script, even though I have "Revenue" and "Member" checked when I created the new issue.  

Also, can you point me to a resource to use to understand which methods are available to me based on whether I am running a normal script or a listener script?  The way that I get and set variables seems to be wildly different when I check your example scripts for both of those on the Adaptavist site and I've yet to find a resource that tells me what to use and when.  Thanks!

 

Jeff

Suggest an answer

Log in or Sign up to answer