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;
Hello @Mohan Sundar ,
which types have your custom fields?
Where do you use this script? Is it post function?
Thank you.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Atlassian Government Cloud has achieved FedRAMP Authorization at the Moderate level! Join our webinar to learn how you can accelerate mission success and move work forward faster in cloud, all while ensuring your critical data is secure.
Register NowOnline forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.