Forums

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

BigDecimal cannot be cast to Double in Scriptrunner Listener

Christian_Grüner June 2, 2020

Hey guys,

 

first of: Sorry I am not a SW developer, therefore please excuse any rooky mistakes in groovy.

I have an issue with refers to assigning the variable "BigDecimal" to Double as in the following error message: 

2020-06-02 20:49:20,537 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.Double at com.atlassian.jira.issue.customfields.impl.NumberCFType.getDbValueFromObject(NumberCFType.java:45)

 

I identified that the error happens for issues where certain variables are initialized interesting enough I am not excluding those codelines at the start.

 

Let me show in more detail. 

First of all the whole error message:

2020-06-02 20:49:20,536 ERROR [runner.AbstractScriptListener]: *************************************************************************************
2020-06-02 20:49:20,537 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null
java.lang.ClassCastException: java.math.BigDecimal 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.call(Unknown Source)
	at Script373.run(Script373.groovy:251)

 

And as you might see in the first lines of the code I excluding the rest of it:
in the first lines I excluding all issues which do not belong to the so called "expand -xx" side. But for all issues that are excluded the log throws the error message for the the groovy line:251 -- which is:

cf_PCM20.updateValue(null, mutableIssue, new ModifiedValue(mutableIssue.getCustomFieldValue(cf_PCM20), PCM20),changeHolder)


Here you can find the whole code:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.event.issue.AbstractIssueEventListener;
import com.atlassian.jira.event.issue.IssueEvent;
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.index.IssueIndexingService;
import com.atlassian.jira.util.ImportUtils;
//import org.apache.log4j.Category


//def log = Logger.getLogger("com.acme.workflows")
//log.setLevel(Level.DEBUG)
//log.warn("Workflow function running...")


def issueManager = ComponentAccessor.issueManager
def issue = event.issue
MutableIssue mutableIssue = issueManager.getIssueObject(issue.key)
def CustomFieldManager = ComponentAccessor.customFieldManager




def cf_measuretype = CustomFieldManager.getCustomFieldObjectByName("Type of measure (lever)")
def measuretype = mutableIssue.getCustomFieldValue(cf_measuretype)
def measuretype_string = measuretype.toString()


if (measuretype_string == "Expand - censored" || measuretype_string == "Expand - censored" || measuretype_string == "Expand - censored" ) {


// ------------------------
// ------------------------ Initialize variables ---
// ------------------------

// Cumulated overall profit improvement contribution as final value -- initialize as 0
def cf_COPICFY20 = CustomFieldManager.getCustomFieldObjectByName("Cumulated overall profit Improvement (contribution) FY20")
def cf_COPICFY21 = CustomFieldManager.getCustomFieldObjectByName("Cumulated overall profit Improvement (contribution) FY21")
def cf_COPICFY22 = CustomFieldManager.getCustomFieldObjectByName("Cumulated overall profit Improvement (contribution) FY22")
def cf_COPICFY23 = CustomFieldManager.getCustomFieldObjectByName("Cumulated overall profit Improvement (contribution) FY23")
def cf_COPICFY24 = CustomFieldManager.getCustomFieldObjectByName("Cumulated overall profit Improvement (contribution) FY24")
def cf_COPICFY25 = CustomFieldManager.getCustomFieldObjectByName("Cumulated overall profit Improvement (contribution) FY25")

def COPICFY20 = mutableIssue.getCustomFieldValue(cf_COPICFY20) as Double
def COPICFY21 = mutableIssue.getCustomFieldValue(cf_COPICFY21) as Double
def COPICFY22 = mutableIssue.getCustomFieldValue(cf_COPICFY22) as Double
def COPICFY23 = mutableIssue.getCustomFieldValue(cf_COPICFY23) as Double
def COPICFY24 = mutableIssue.getCustomFieldValue(cf_COPICFY24) as Double
def COPICFY25 = mutableIssue.getCustomFieldValue(cf_COPICFY25) as Double


// Cumulated planned profit contribution as value to gain overall profit contribution -- initialize as 0
def cf_CPPC20 = CustomFieldManager.getCustomFieldObjectByName("Cumulated planned profit contribution FY20")
def cf_CPPC21 = CustomFieldManager.getCustomFieldObjectByName("Cumulated planned profit contribution FY21")
def cf_CPPC22 = CustomFieldManager.getCustomFieldObjectByName("Cumulated planned profit contribution FY22")
def cf_CPPC23 = CustomFieldManager.getCustomFieldObjectByName("Cumulated planned profit contribution FY23")
def cf_CPPC24 = CustomFieldManager.getCustomFieldObjectByName("Cumulated planned profit contribution FY24")
def cf_CPPC25 = CustomFieldManager.getCustomFieldObjectByName("Cumulated planned profit contribution FY25")

def CPPC20 = mutableIssue.getCustomFieldValue(cf_CPPC20) as Double
def CPPC21 = mutableIssue.getCustomFieldValue(cf_CPPC21) as Double
def CPPC22 = mutableIssue.getCustomFieldValue(cf_CPPC22) as Double
def CPPC23 = mutableIssue.getCustomFieldValue(cf_CPPC23) as Double
def CPPC24 = mutableIssue.getCustomFieldValue(cf_CPPC24) as Double
def CPPC25 = mutableIssue.getCustomFieldValue(cf_CPPC25) as Double


//Profit contribution maring to calculate planned profit contribution -- initialize as 0
def cf_PCM20 = CustomFieldManager.getCustomFieldObjectByName("Profit contribution margin FY20")
def cf_PCM21 = CustomFieldManager.getCustomFieldObjectByName("Profit contribution margin FY21")
def cf_PCM22 = CustomFieldManager.getCustomFieldObjectByName("Profit contribution margin FY22")
def cf_PCM23 = CustomFieldManager.getCustomFieldObjectByName("Profit contribution margin FY23")
def cf_PCM24 = CustomFieldManager.getCustomFieldObjectByName("Profit contribution margin FY24")
def cf_PCM25 = CustomFieldManager.getCustomFieldObjectByName("Profit contribution margin FY25")

def PCM20 = mutableIssue.getCustomFieldValue(cf_PCM20) as Double
def PCM21 = mutableIssue.getCustomFieldValue(cf_PCM21) as Double
def PCM22 = mutableIssue.getCustomFieldValue(cf_PCM22) as Double
def PCM23 = mutableIssue.getCustomFieldValue(cf_PCM23) as Double
def PCM24 = mutableIssue.getCustomFieldValue(cf_PCM24) as Double
def PCM25 = mutableIssue.getCustomFieldValue(cf_PCM25) as Double


// ------------------------
// ------------------------ Import variables ---
// ------------------------

// Profit maring to to calculate profit contribution margin -- initialize as 0 if not set yet
def cf_PM20 = CustomFieldManager.getCustomFieldObjectByName("Profit (EBITA) - Marge [%] FY20")
def cf_PM21 = CustomFieldManager.getCustomFieldObjectByName("Profit (EBITA) - Marge [%] FY21")
def cf_PM22 = CustomFieldManager.getCustomFieldObjectByName("Profit (EBITA) - Marge [%] FY22")
def cf_PM23 = CustomFieldManager.getCustomFieldObjectByName("Profit (EBITA) - Marge [%] FY23")
def cf_PM24 = CustomFieldManager.getCustomFieldObjectByName("Profit (EBITA) - Marge [%] FY24")
def cf_PM25 = CustomFieldManager.getCustomFieldObjectByName("Profit (EBITA) - Marge [%] FY25")

def PM20 = mutableIssue.getCustomFieldValue(cf_PM20) as Double
def PM21 = mutableIssue.getCustomFieldValue(cf_PM21) as Double
def PM22 = mutableIssue.getCustomFieldValue(cf_PM22) as Double
def PM23 = mutableIssue.getCustomFieldValue(cf_PM23) as Double
def PM24 = mutableIssue.getCustomFieldValue(cf_PM24) as Double
def PM25 = mutableIssue.getCustomFieldValue(cf_PM25) as Double



//Revenue to calculate planned profit contribution -- initialize as 0 if not set yet
def cf_rev20 = CustomFieldManager.getCustomFieldObjectByName("Revenue according to POC per FY20")
def cf_rev21 = CustomFieldManager.getCustomFieldObjectByName("Revenue according to POC per FY21")
def cf_rev22 = CustomFieldManager.getCustomFieldObjectByName("Revenue according to POC per FY22")
def cf_rev23 = CustomFieldManager.getCustomFieldObjectByName("Revenue according to POC per FY23")
def cf_rev24 = CustomFieldManager.getCustomFieldObjectByName("Revenue according to POC per FY24")
def cf_rev25 = CustomFieldManager.getCustomFieldObjectByName("Revenue according to POC per FY25")

def rev20 = mutableIssue.getCustomFieldValue(cf_rev20) as Double
def rev21 = mutableIssue.getCustomFieldValue(cf_rev21) as Double
def rev22 = mutableIssue.getCustomFieldValue(cf_rev22) as Double
def rev23 = mutableIssue.getCustomFieldValue(cf_rev23) as Double
def rev24 = mutableIssue.getCustomFieldValue(cf_rev24) as Double
def rev25 = mutableIssue.getCustomFieldValue(cf_rev25) as Double


//cumulated net savings -- initialize as 0 if not set yet
def cf_CNSFY20 = CustomFieldManager.getCustomFieldObjectByName("Cumulated net savings FY20")
def cf_CNSFY21 = CustomFieldManager.getCustomFieldObjectByName("Cumulated net savings FY21")
def cf_CNSFY22 = CustomFieldManager.getCustomFieldObjectByName("Cumulated net savings FY22")
def cf_CNSFY23 = CustomFieldManager.getCustomFieldObjectByName("Cumulated net savings FY23")
def cf_CNSFY24 = CustomFieldManager.getCustomFieldObjectByName("Cumulated net savings FY24")
def cf_CNSFY25 = CustomFieldManager.getCustomFieldObjectByName("Cumulated net savings FY25")

def CNSFY20 = mutableIssue.getCustomFieldValue(cf_CNSFY20) as Double
def CNSFY21 = mutableIssue.getCustomFieldValue(cf_CNSFY21) as Double
def CNSFY22 = mutableIssue.getCustomFieldValue(cf_CNSFY22) as Double
def CNSFY23 = mutableIssue.getCustomFieldValue(cf_CNSFY23) as Double
def CNSFY24 = mutableIssue.getCustomFieldValue(cf_CNSFY24) as Double
def CNSFY25 = mutableIssue.getCustomFieldValue(cf_CNSFY25) as Double




//Import module
def cf_module = CustomFieldManager.getCustomFieldObjectByName("Module/department")
def module = mutableIssue.getCustomFieldValue(cf_module)
def module_string = module.toString()

// ------------------------
// ------------------------ Start calculations ---
// ------------------------


//set Profit contribution as defined through logic on module level

if (module_string == "censored") {
PCM20 = PM20 - 6.0
PCM21 = PM21 + 6.0
PCM22 = PM22 - 6.0
PCM23 = PM23 - 6.0
PCM24 = PM24 + 6.0
PCM25 = PM25 + 6.0
}

if (module_string == "censored") {
PCM20 = PM20 - 6.0
PCM21 = PM21 - 6.0
PCM22 = PM22 - 6.0
PCM23 = PM23 - 6.0
PCM24 = PM24 - 6.0
PCM25 = PM25 + 6.0
}

if (module_string == "censored") {
//Set all to zero for ...
PCM20 = PM20-PM20
PCM21 = PM20-PM20
PCM22 = PM20-PM20
PCM23 = PM20-PM20
PCM24 = PM20-PM20
PCM25 = PM20-PM20
}


// Margins are set -> calculate contribution -- Right now profit marings are absolute values and not decimals
// Profit contribution = (cumulated revenue per FY) * profit contribution margin / 100 --> Therefore automatically cumulated

CPPC20 = rev20 * PCM20/100
CPPC21 = (rev20 + rev21) * PCM21/100
CPPC22 = (rev20 + rev21 + rev22) * PCM22/100
CPPC23 = (rev20 + rev21 + rev22 + rev23) * PCM23/100
CPPC24 = (rev20 + rev21 + rev22 + rev23 + rev24) * PCM24/100
CPPC25 = (rev20 + rev21 + rev22 + rev23 + rev24 + rev25) * PCM25/100

// Cumulated profit contribution calculated -> calculate overall profit contribution
// Overaall porift improvement contribtuion = planned profit contribution + net savings
COPICFY20 = CPPC20 + CNSFY20
COPICFY21 = CPPC21 + CNSFY21
COPICFY22 = CPPC22 + CNSFY22
COPICFY23 = CPPC23 + CNSFY23
COPICFY24 = CPPC24 + CNSFY24
COPICFY25 = CPPC25 + CNSFY25




// ------------------------
// ------------------------ Update Jira Fields through changeholder and re-indexing ---
// ------------------------

def changeHolder = new DefaultIssueChangeHolder()

// update planned profit contribution
cf_PCM20.updateValue(null, mutableIssue, new ModifiedValue(mutableIssue.getCustomFieldValue(cf_PCM20), PCM20),changeHolder)
cf_PCM21.updateValue(null, mutableIssue, new ModifiedValue(mutableIssue.getCustomFieldValue(cf_PCM21), PCM21),changeHolder)
cf_PCM22.updateValue(null, mutableIssue, new ModifiedValue(mutableIssue.getCustomFieldValue(cf_PCM22), PCM22),changeHolder)
cf_PCM23.updateValue(null, mutableIssue, new ModifiedValue(mutableIssue.getCustomFieldValue(cf_PCM23), PCM23),changeHolder)
cf_PCM24.updateValue(null, mutableIssue, new ModifiedValue(mutableIssue.getCustomFieldValue(cf_PCM24), PCM24),changeHolder)
cf_PCM25.updateValue(null, mutableIssue, new ModifiedValue(mutableIssue.getCustomFieldValue(cf_PCM25), PCM25),changeHolder)



// Redinex the issue
boolean isIndex = ImportUtils.isIndexIssues();
ImportUtils.setIndexIssues(true);
IssueIndexingService IssueIndexingService = (IssueIndexingService) ComponentAccessor.getComponent(IssueIndexingService.class);
IssueIndexingService.reIndex(mutableIssue);

ImportUtils.setIndexIssues(isIndex);

} //close if measurelever statement

 

All tips are greatly appreciated.
I am using Jira v8.0.3 and scriptrunner v6.1.0-p5

 

BR

CG

 

 

 

1 answer

1 accepted

0 votes
Answer accepted
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.
June 2, 2020

You can't cast because the data types are incompatible in Java (not Scriptrunner or Jira).

Use .doubleValue() to attempt the conversion, and see https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html

Christian_Grüner June 2, 2020

Hey Nic,

I didnot initialize those variables with BigDecimals.
They are just plain "customfields" of the type "numberfield" in Jira.

As you see the variables I am using in my script are all doubles.

Best regards
Christian

Suggest an answer

Log in or Sign up to answer