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

It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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
Community showcase
Published in Confluence Cloud

🎨 Add some visual life to your templates

Hi Atlassian Community, My name is Avni Barman, and I am a Product Manager on the Confluence Cloud team. Based on feedback from you, we are giving admins more power to create templates that a...

183 views 3 8
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you