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]
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
:) Glad it was helpful
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You need to check for null values and handle the exception. Let me give you a complete code.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.