ScriptRunner Scripted Field Error - Dividing two fields

Travis Reynolds February 7, 2020

Hello

  I am a real novice at groovy scripting and I am running into this error.  I just need to divide the two fields listed and return that number.  My code:

 

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

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()
def cf1 = customFieldManager.getCustomFieldObject("Cost of Delay")
def delay = (double) getCustomFieldValue(cf1)
def cf2 = customFieldManager.getCustomFieldObject("Job Size")
def size = (double) getCustomFieldValue(cf2)

return delay/size

 

Error I am currently getting in Script Console

Error

No signature of method: org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.getCustomFieldValue() is applicable for argument types: (null) values: [null]

4 answers

1 accepted

0 votes
Answer accepted
Ravi Sagar _Sparxsys_
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.
February 10, 2020

Hi @Travis Reynolds 

Try this code. It should work in console where there is a text issue id (containing those fields).

 

import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue

IssueManager im = ComponentAccessor.getIssueManager()
MutableIssue issue = im.getIssueObject("ANDROID-48")

if(issue){

//get the value
def customFieldManager = ComponentAccessor.getCustomFieldManager()

//CoD
def codField = customFieldManager.getCustomFieldObjectsByName("Cost of Delay")
def codFieldValue = issue.getCustomFieldValue(codField[0]) as Double

//Job Size
def jsField = customFieldManager.getCustomFieldObjectsByName("Job Size")
def jsFieldValue = issue.getCustomFieldValue(jsField[0]) as Double

if (codFieldValue == null || jsFieldValue == null) {

return "Invalid/Cannot be null"
}
return codFieldValue / jsFieldValue





}else {

return "Issue doesn't exist"
}

 

You can also use this code in scripted field where the issue object is not needed.

 

Ravi

Travis Reynolds February 11, 2020

Ravi

  Thank you so much for the help!  This works great with testing in console and then removing the issue check for the actual scripted field.  No errors, and the values are showing up correctly.  

 

Thanks again!

Travis

Like Ravi Sagar _Sparxsys_ likes this
Ravi Sagar _Sparxsys_
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.
February 11, 2020

:) Glad it was helpful

1 vote
Jack Nolddor _Sweet Bananas_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
February 7, 2020

Hello Travis,

 

Could you please to change the following lines:

 

def delay = (double) getCustomFieldValue(cf1)
def size = (double) getCustomFieldValue(cf2)

 

...and use this line instead?

 

def delay = issue.getCustomFieldValue(cf1) as Double
def size = issue.getCustomFieldValue(cf2) as Double

 

Please, tell us the result :)

Kind Regards

0 votes
Travis Reynolds February 7, 2020

Hey @Jack Nolddor _Sweet Bananas_  and @Ravi Sagar _Sparxsys_   Thanks for the replies.  So I tried the below in my code:

 

def delay = issue.getCustomFieldValue(cf1) as Double
def size = issue.getCustomFieldValue(cf2) as Double

 and I get this error in the script console: The variable issue is undeclared, which I am assuming is something that I am not importing at the beginning.  I tried to add the :

IssueManager im = ComponentAccessor.getIssueManager()

To the beginning of the code but I still get an error:

Error: Enable to resolve class IssueManager

 

So I thought that it might not be working in Script Console but it might work in JIRA scripted field, so I tested that and got this error:

ERROR: 

java.lang.NullPointerException
at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896)
at com.atlassian.jira.issue.Issue$getCustomFieldValue$1.call(Unknown Source)
at Script158.run(Script158.groovy:7)

 

So to summarize, I tested in the scripted field this code:

 

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

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager()

def cf1 = customFieldManager.getCustomFieldObject("Cost of Delay")
def delay = issue.getCustomFieldValue(cf1) as Double
def cf2 = customFieldManager.getCustomFieldObject("Job Size")
def size = issue.getCustomFieldValue(cf2) as Double

return delay/size

 

And got this error:

ERROR: 

java.lang.NullPointerException
at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896)
at com.atlassian.jira.issue.Issue$getCustomFieldValue$1.call(Unknown Source)
at Script158.run(Script158.groovy:7)

 

 

The fields are both numerical values if that helps.  Again, I appreciate the assistance on this.

Jack Nolddor _Sweet Bananas_
Marketplace Partner
Marketplace Partners provide apps and integrations available on the Atlassian Marketplace that extend the power of Atlassian products.
February 7, 2020

Please, move your code to a Scripted fields instead of using the Script Console since in the Script Console the issue variable is not available.

Travis Reynolds February 10, 2020

Hey Jack

  I tried this and I get the error:

java.lang.NullPointerException
at com.atlassian.jira.issue.IssueImpl.getCustomFieldValue(IssueImpl.java:896)
at com.atlassian.jira.issue.Issue$getCustomFieldValue$1.call(Unknown Source)
at Script158.run(Script158.groovy:7)

 

Thanks

Ravi Sagar _Sparxsys_
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.
February 10, 2020

Hi @Travis Reynolds 

You need to check for null values and handle the exception. Let me give you a complete code.

0 votes
Ravi Sagar _Sparxsys_
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.
February 7, 2020

Hi @Travis Reynolds 

As @Jack Nolddor _Sweet Bananas_ mentioned above you need the issue object in your code. If you are testing code in your console you can use something like this to get your issue.

IssueManager im = ComponentAccessor.getIssueManager()
MutableIssue issue = im.getIssueObject("Test-1")

In other places you may have your issue object already like post function or listeners.

Ravi 

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events