Are you in the loop? Keep up with the latest by making sure you're subscribed to Community Announcements. Just click Watch and select Articles.

×
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

SUM the values of 2 Number Custom Fields in a 3rd Custom Scripted Field (ScriptRunner)

Edited
Z B
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.
Dec 11, 2019

I have 2 Number Custom Fields, let's say, they are called "11" and "22", and I want to display their sum amount in a 3rd custom field which is a scripted field.

I added the script below to the Script Field:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.user.ApplicationUser

def issueManager = ComponentAccessor.getIssueManager()
def linkManager = ComponentAccessor.getIssueLinkManager()
def cfm = ComponentAccessor.getCustomFieldManager()

Issue issueKey = issue
def id=issueKey.getId()

def customFieldManager = ComponentAccessor.getCustomFieldManager();

def cf = customFieldManager.getCustomFieldObject("11")
def cff = customFieldManager.getCustomFieldObject("22")

def 1 = getCustomFieldValue(cf)
def 2 = getCustomFieldValue(cff)

def n = Integer.parseInt("1")
def b = Integer.parseInt("2")
 

if (n && b){
    def sumValue =  n + b
    return sumValue
}

 In the console no error is displayed, but when I run it, I get an error log:

--------------------------------------------

2019-12-11 11:09:36,549 ERROR [runner.ScriptFieldPreviewRunner]: *************************************************************************************
2019-12-11 11:09:36,551 ERROR [runner.ScriptFieldPreviewRunner]: Script field preview failed for field that has not yet been created
groovy.lang.MissingMethodException: No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getCustomFieldValue() is applicable for argument types: (null) values: [null]
at Script906.run(Script906.groovy:67)

--------------------------------------------

Is this because I didn't manage to access the custom field value at all or as an integer? What would be the solution?

Thank you in advance!

4 answers

1 accepted

Suggest an answer

Log in or Sign up to answer
3 votes
Answer accepted
Z B
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.
Dec 12, 2019 • edited

@Peter Garncarek

 @Leonard Chew 

So here is the version which now works for me. Replace id1 and id2 with your actual custom field IDs.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.search.SearchProvider
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.user.ApplicationUser

def issueManager = ComponentAccessor.getIssueManager()
def linkManager = ComponentAccessor.getIssueLinkManager()
def cfm = ComponentAccessor.getCustomFieldManager()

Issue issueKey = issue
def id=issueKey.getId()

def customFieldManager = ComponentAccessor.getCustomFieldManager();

def cf = customFieldManager.getCustomFieldObject(id1)
def cff = customFieldManager.getCustomFieldObject(id2)

def n = Integer.parseInt((issue.getCustomFieldValue(cf) ?: 0).toString().replaceAll(~/[.].*/, ""))
def b = Integer.parseInt((issue.getCustomFieldValue(cff) ?: 0).toString().replaceAll(~/[.].*/, ""))

Integer sum = (n ?: 0) + (b ?: 0);

After going through many other community posts for adding two custom fields and storing into 3rd field, your script worked like a charm!! I could not get the result in any other script as I was getting error incorrect method used though I have written all required imports.

 

Thankyou!

Could you help me with the error "cannot assign value of type java.math.BigDecimal to variable of type java.lang.integer"

 

I'm trying to find the average after adding two custom fields and as the return value is in Decimal I'm getting this error.

 

Please advise how to rectify this in your code.

I had a similar challenge and found enough example to write this script for totaling many number fields.  Change my field names to yours and see if it works.  The fields are just number fields.  You will see an error on def cf = line in editor but it works outside the editor.

I have this scripted field called "PPD Total Hours" which has this script.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManagerCustomFieldManager

CustomFieldManager customFieldManager = componentAccessor.getCustomFieldManager()

List<String> fieldNames = ["Mechanical engineering","mech engineering cranes & tools","mech engineering LCS","mech engineering ECH","mech engineering WRH","mech engineering WINCH","Electrical engineering","Documentation","Sales configurators & E2E","Product management","Product lifecycle management","Other"]

List<Integer> scores = []
for (String field : fieldNames){
def cf = customFieldManager.getCustomFieldObjectByName(field)
scores.add((Integer) issue.getCustomFieldValue(cf)?: 0)
}
return scores.sum {it}
Z B
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.
Dec 12, 2019 • edited

Hi @Peter Garncarek ,

thank you very much for the comment! In the meantime I have actually received a solution from a different source.

I'm curios about your version too, so I tried it, but for the 2nd line I get an unable to resolve class error msg. I guess "CustomFieldManagerCustomFieldManager" is incorrect?

But if I change that to only "CustomFieldManager", I still get the error "the variable CustomFieldManager is undeclared"

Error in the way I posted the code (my first time trying to do so).  I am on Jira server 8.5.1 and latest version of Scriptrunner. I have corrected the original post to have this line

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

This is the way it looks in my script window.  I do not know why I see the error but the script works.

Capture.JPG

Z B
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.
Dec 12, 2019

Thanks, I have it like this now, and I get "The variable [issue] is undeclared". How would I declare / import it?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

List<String> fieldNames = ["1","2"]

List<Integer> scores = []
for (String field : fieldNames){
def cf = customFieldManager.getCustomFieldObjectsByName("3")
scores.add((Integer) issue.getCustomFieldValue(cf)?: 0)
}
return scores.sum {it}

This line, change it to exactly this.  Only thing to customize from the original script are the field names in List<string> fieldnames = []

def cf = customFieldManager.getCustomFieldObjectsByName(field)

So what you are doing is adding the number in field "1" and field "2" into this scripted field.  

Z B
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.
Dec 12, 2019

I already had it like that. There is a problem with

 scores.add((Integer) issue.getCustomFieldValue(cf)?: 0)

because of the part "issue" I'm getting an error msg: "The variable [issue] is undeclared"

I have no idea why this happens.  I am new to Scriptrunner but have the same issue as you when trying scripts from posts I find.  Undeclared is one of themost common errors.   What happens if you just try it by using Preview and entering the issue number.  Does it work?

Z B
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.
Dec 12, 2019

No, it gives a null and an error.

Thank you for the input anyway!

At least I know now it is not just me that has problem running scripts that people post, even if it works for them.  I will be working with Scriptrunner more next year so maybe I will figure out why.  Sorry it did not work for you.

Hi Peter & ZB, this is really helpful.

I was able to get this to work, however, now I also want to have another field value change based on the calculated field's value. How do I make it work? 

example : if total value is between 1-49 then it is Green, if its between 50 and 75 it will be green, then if it is above 76 then its red.

Like Z B likes this
Z B
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.
Jan 08, 2020

Hi, I'm glad it helped.

What you are mentioning now is quite different, I'm not well-versed in ScriptRunner so I wouldn't be able to give you a solution.

I think you could search for something along the lines such as

https://community.atlassian.com/t5/Marketplace-Apps-Integrations/How-to-set-custom-field-value-based-on-another-custom-field/qaq-p/684400

and you may search in https://library.adaptavist.com/search?apps=scriptrunner

Hi Z_B. Can you please post your working solution? thanks.

Z B
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.
Apr 28, 2020

Hi, I have this substraction  now:

 

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.fields.CustomField;
CustomField a= ComponentAccessor.getCustomFieldManager().getCustomFieldObject(000);
CustomField b= ComponentAccessor.getCustomFieldManager().getCustomFieldObject(000);
Integer x = (Integer) issue.getCustomFieldValue(a);
Integer y = (Integer) issue.getCustomFieldValue(b)
Integer diference = (x ?: 0) - (y ?: 0);
return diference

Any chance someone can post an entire working script for summing two fields and putting the total in a third field?  I'm so weak at scripting I need the entire script.  Thank you in advance to anyone who can help me. @Peter Garncarek   @Z B 

I have the same need as the original poster -- I have number fields I simply want to sum in a "Total" field.  I have script runner.  Can someone post for me the entire script that I could use to make this work?  I'm a weak scripter and need the entire script, not just excerpts.  Thanks you so much to anyone who can help.

0 votes
Leonard Chew
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.
Dec 12, 2019

I hear you! - One of the most annoying things in scripting with Scriptrunner and groovy is casting the numbers!

Try this workaround:

def n = Integer.parseInt((issue.getCustomFieldValue(cf) ?: 0).toString().replaceAll(~/[.].*/, ""))
def b = Integer.parseInt((issue.getCustomFieldValue(cff) ?: 0).toString().replaceAll(~/[.].*/, ""))

It gets the numeric value of the field, transforms it into a String, cuts of the decimal (.0) with a regular expression and then finally casts the string to an integer. 

I'm sure there is a better solution, but at the time I needed to cast it, it was the solution that simply worked and so I just let it be.

Z B
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.
Dec 12, 2019

Thank you, with this I managed to solve the parse issue.

There was still another problem, the way I expressed the sum amount was also incorrect, it just didn't take null into consideration. From another source I was given a solution for that:

Integer diference = (n ?: 0) + (b ?: 0);
Like Peter Garncarek likes this
Leonard Chew
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.
Dec 12, 2019

The null handling is already done (if null then it gets the value '0')

But I have just seen that my answer is crap. Stick to what @Peter Garncarek has suggested, because it is simpler and works :-)

def n = (Integer)(issue.getCustomFieldValue(cf) ?: 0)
def b = (Integer)(issue.getCustomFieldValue(cff) ?: 0)

if cf or cff are empty, their values will be 0, so there is no need to check any further.

Like Peter Garncarek likes this
TAGS
AUG Leaders

Atlassian Community Events