Just a heads up: On March 24, 2025, starting at 4:30pm CDT / 19:30 UTC, the site will be undergoing scheduled maintenance for a few hours. During this time, the site might be unavailable for a short while. Thanks for your patience.

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

Adding and Dividing two Custom fields and pass the output to third custom field as Decimal output

Mohan Sundar
Contributor
December 10, 2020

Could you help me with the error "cannot assign value of type java.math.BigDecimal to variable of type java.lang.integer"

 

I'm trying to find the average after adding two custom fields and as the return value is in Decimal I'm getting this error.

 

Please advise how to rectify this in this below code.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.user.ApplicationUser

def issueManager = ComponentAccessor.getIssueManager()
def linkManager = ComponentAccessor.getIssueLinkManager()
def cfm = ComponentAccessor.getCustomFieldManager()

Issue issueKey = issue
def id=issueKey.getId()

def customFieldManager = ComponentAccessor.getCustomFieldManager();

def cf = customFieldManager.getCustomFieldObject(customfieldID1)
def cff = customFieldManager.getCustomFieldObject(customfieldID2)

def n = Integer.parseInt((issue.getCustomFieldValue(cf) ?: 0).toString().replaceAll(~/[.].*/, ""))
def b = Integer.parseInt((issue.getCustomFieldValue(cff) ?: 0).toString().replaceAll(~/[.].*/, ""))

Integer sum = (n ?: 0) + (b ?: 0) / 2;

 

1 answer

0 votes
Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 10, 2020

Hello @Mohan Sundar ,

which types have your custom fields?

Where do you use this script? Is it post function?

Thank you.

Mohan Sundar
Contributor
December 11, 2020

Hi Hana,

 

I'm using Single Select list type custom field where the values are numbers 1 to 5.

I want to create a Scriptrunner scripted field with the above script. Hence the scripted field should display output of FieldA+FieldB/2 

 

Right now I'm getting the value but since my options are 1-5 when divided by 2 the output will be of decimal which I'm unable to get it.

 

Your assistance is appreciated! Thanks in advance.

 

 

Regards,

Mohana Sundar J

Hana Kučerová
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 11, 2020

Hi @Mohan Sundar ,

please try something like this:

xxxxx and yyyyy needs to be replaced with ids of your single select list custom fields

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.customfields.option.Option
import com.atlassian.jira.issue.fields.CustomField

Long customFieldId1 = xxxxx
Long customFieldId2 = yyyyy

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

CustomField customField1 = customFieldManager.getCustomFieldObject(customFieldId1)
CustomField customField2 = customFieldManager.getCustomFieldObject(customFieldId2)

Option customFieldOption1 = issue.getCustomFieldValue(customField1) as Option
Option customFieldOption2 = issue.getCustomFieldValue(customField2) as Option

if (!customFieldOption1 || !customFieldOption2) {
return 0
}

String customFieldValue1 = customFieldOption1.getValue() as String
String customFieldValue2 = customFieldOption2.getValue() as String

Integer number1 = customFieldValue1?.isInteger() ? customFieldValue1 as Integer : 0
Integer number2 = customFieldValue2?.isInteger() ? customFieldValue2 as Integer : 0

return (number1 + number2) / 2
Like Mohana Sundar J likes this

Suggest an answer

Log in or Sign up to answer