Missed Team ’24? Catch up on announcements here.

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

Scriptrunner - Scripted field to store value in another custom field

Peter Garncarek October 2, 2020

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

Suggest an answer

Log in or Sign up to answer
0 votes
Raziman Dom - Ricksoft
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 7, 2020

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. 

TAGS
AUG Leaders

Atlassian Community Events