Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Sum two custom fields and assign a value to a third custom field

arno
Contributor
December 4, 2023

Hi All

I created three custom fields for statistical data, in which the first and second fields are filled in manually by the user, and the third field automatically obtains the sum of the values of the first two fields, but I do not know how to assign values, the following is my code


import com.atlassian.jira.component.ComponentAccessor
def customFieldManager = ComponentAccessor.customFieldManager
def filed = customFieldManager.getCustomFieldObject('customfield_14300')
def filed1 = customFieldManager.getCustomFieldObject('customfield_14301')
def filed2 = customFieldManager.getCustomFieldObject('customfield_14302')


def a = issue.getCustomFieldValue(filed)
def b = issue.getCustomFieldValue(filed1)

I try to use
def c = a+b
issue.setCustomFieldValue(filed2,c)
I got an error when I did this. I tried to convert the field value using "toDobule ()", but it doesn't seem to allow me to do so
All three custom fields are numeric fields (not text fields). How can I optimize the code to achieve the effect I want

2 answers

1 vote
Florian Bonniec
Community Champion
December 4, 2023

Hi @arno 

 

Instead of using Scriptrunner, did you try automation for JIRA if you have it ? It's usually easier to use this app for simple usecase like that.

 

Jira smart values - math expressions | Automation for Jira Data Center and Server 9.0 | Atlassian Documentation

 

If you need to use a script please have a look to 

Updates custom fields on an issue, without affecting change history or last update time - Adaptavist Library

 

Regards

0 votes
Bobby Bailey
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 4, 2023

Hi @arno , 

I am not sure which feature you are using to achieve this, it looks like it might be a Listener, but this is something the Script Fields feature was built for. Firstly, just so we are on the same page, your current script can be made simpler using HAPI.

With HAPI, to get the custom fields, all you need to do is call the .getCustomFieldValue, you don't need the first section with:

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.customFieldManager

def filed = customFieldManager.getCustomFieldObject('customfield_14300')

def filed1 = customFieldManager.getCustomFieldObject('customfield_14301')

def filed2 = customFieldManager.getCustomFieldObject('customfield_14302')


As HAPI handles that for you. Now, to update the issue with HAPI, you can find examples here, but the code looks like this:

issue.update { 

    setCustomFieldValue('filed2', c)

 }

 
However, as I mentioned, this functionality is really something the Script Fields feature was build for. I have written a script to perform the functionality you require in a Script Field, which you can find here:

https://bitbucket.org/Adaptavist/workspace/snippets/Kdpy4e

Let me know if this helps! 

Kind regards, 

Bobby

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
AUG Leaders

Atlassian Community Events