Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Deleted user
0 / 0 points
Next:
badges earned

Your Points Tracker
Challenges
Leaderboard
  • Global
  • Feed

Badge for your thoughts?

You're enrolled in our new beta rewards program. Join our group to get the inside scoop and share your feedback.

Join group
Recognition
Give the gift of kudos
You have 0 kudos available to give
Who do you want to recognize?
Why do you want to recognize them?
Kudos
Great job appreciating your peers!
Check back soon to give more kudos.

Past Kudos Given
No kudos given
You haven't given any kudos yet. Share the love above and you'll see it here.

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

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

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!

3 answers

1 accepted

1 vote
Answer accepted

@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}

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

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.  

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?

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

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.

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
0 votes
Leonard Chew Community Leader 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.

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 Community Leader 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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Marketplace Apps & Integrations

Staying organized with Jira: best practices for a better project management

Project managers know this problem: A “mountain of work” lays in front of you, and you don’t know how and where to tackle them. Different to-dos lie ahead, but just one task after the other can be ha...

177 views 2 1
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