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

Next challenges

Recent achievements

Recognition

  • Give kudos
  • My kudos

Leaderboard

  • Global

Trophy case

Kudos (beta program)

Kudos logo

You've been invited into the Kudos (beta program) private group. Chat with others in the program, or give feedback to Atlassian.

View group

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

(SOLVED) ScriptRunner Scripted Field Error - Dividing two fields Edited

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

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

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 _Adaptavist_ likes this

:) Glad it was helpful

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

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 

Hey @Jack Nolddor _Sweet Bananas_  and @Ravi Sagar _Adaptavist_   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.

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.

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

Hi @Travis Reynolds 

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

Suggest an answer

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

5 mobile apps for Jira Cloud to boost productivity

  It’s very important to have access to the workflow process from anywhere. Especially if you manage the work of others. There is no difference whether you’re out of office, or drive a ca...

284 views 2 5
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