Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,368,231
Community Members
 
Community Events
168
Community Groups

Scriptrunner - Scripted field to store value in another custom field

I have a requirement where I need to add values from multiple number fields and perform math operation on them.  Then I need that result stored in another custom field for use in Portfolio (since Portfolio does not support scripted fields).

FieldA = number field

FieldB = number field

cdField = Scripted number field to store sum of FieldA+FieldB

FieldD = number field

jsField = Scripted number field to store result of cdField/jsField

wsjfField = number field

I have worked out the scripts for cdField and jsField but have two problems to overcome:

1.  When I set searcher for wsjfField to "Number Searcher" this line has an error in the script editor (red squiggly under / ):
     return FieldC / FieldD

If I set the searcher to none it works without error

2.  On jsField script I need to have code that takes the return value that takes that calculated value and updates another custom field (wsjfField) to that value (wsjfValue) so it can be used in Portfolio.

 

This is my relevant code for jsField:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

CustomField jsField = customFieldManager.getCustomFieldObject("customfield_15123")

CustomField cdField = customFieldManager.getCustomFieldObject("customfield_15126")

CustomField wsjfField = customFieldManager.getCustomFieldObject("customfield_15128")

String jsValue = issue.getCustomFieldValue(jsField).toString()
String cdValue = issue.getCustomFieldValue(cdField).toString()

int jsInt = Integer.parseInt(jsValue)
int cdInt = Integer.parseInt(cdValue)


def wsjfValue = cdInt/jsInt

//need code here to update wsjfField value in this issue to wsjfValue

return cdInt / jsInt

 

I have tried various code samples to update custom fields but they all get errors to where I gave up and figure I ask here if this is even possible from a scripted field.

1 answer

0 votes

Hi Peter,

You can fix it by change:

String jsValue = issue.getCustomFieldValue(jsField).toString()
String cdValue = issue.getCustomFieldValue(cdField).toString()

To:

def jsValue = issue.getCustomFieldValue(jsField) as Double
def cdValue = issue.getCustomFieldValue(cdField).toString() as Double

Remove:

//int jsInt = Integer.parseInt(jsValue)
//int cdInt = Integer.parseInt(cdValue)

//def wsjfValue = cdValue/jsValue

Return:

return cdValue/jsValue

So your final code should be:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

CustomField jsField = customFieldManager.getCustomFieldObject("customfield_15123")

CustomField cdField = customFieldManager.getCustomFieldObject("customfield_15126")

CustomField wsjfField = customFieldManager.getCustomFieldObject("customfield_15128")

def jsValue = issue.getCustomFieldValue(jsField) as Double
def cdValue = issue.getCustomFieldValue(cdField) as Double

return cdValue/jsValue

It is recommended to return as Double and not Integer since you are dividing the 2 numbers. 

Suggest an answer

Log in or Sign up to answer
TAGS

Atlassian Community Events