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

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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

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.
Oct 07, 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