I need assistance in creating a scripted field using Scriptrunner to count the number of sprints that an Issue is in. Can anyone help with the Groovy script or have any example of something similar?
Thank you for any help.
Eric
Hi Eric.
Katy gave you a very good example, and it only needed a very slight modification to make it work:
Here is the code for your custom field. I tested it this very morning:
import com.atlassian.jira.component.ComponentAccessor
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Sprint")
def sprints = issue.getCustomFieldValue(cf)
if (sprints) {
return sprints
} else {
return 0
}
Make sure to set a number searcher and template!
If this answer solved your problem, please upvote it and mark it as answered so that other users can know this has been solved. We also would love your feedback in our support revisions in the ScriptRunner Addon page.
Cheers!
DYelamos
We are getting somewhere but it's not working. When I try to use your code I'm getting this error.
2017-12-13 15:22:52,204 ERROR [customfield.GroovyCustomField]: ************************************************************************************* Script field failed on issue: LSA-15446, field: Sprint Count java.lang.Exception: The search indexer: class com.atlassian.jira.issue.customfields.searchers.ExactNumberSearcher expected your script to return a java.lang.Double, but it returned an java.util.ArrayList. We couldn't convert it to a java.lang.Double Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[com.atlassian.greenhopper.service.sprint.Sprint@74b93034[id=602,rapidViewId=247,state=CLOSED,name=LSA: Wk 2017/11/13-11/17,startDate=2017-11-13T10:00:34.976-05:00,endDate=2017-11-20T10:00:00.000-05:00,completeDate=2017-11-20T11:38:53.094-05:00,sequence=546], com.atlassian.greenhopper.service.sprint.Sprint@3c72867a[id=603,rapidViewId=247,state=CLOSED,name=LSA: Wk 2017/11/20-11/24,startDate=2017-11-20T11:39:01.101-05:00,endDate=2017-11-26T11:39:00.000-05:00,completeDate=2017-11-27T11:10:27.194-05:00,sequence=565], com.atlassian.greenhopper.service.sprint.Sprint@5fd130a5[id=604,rapidViewId=247,state=CLOSED,name=LSA: Wk 2017/11/27-12/01,startDate=2017-11-27T11:11:17.937-05:00,endDate=2017-12-03T11:11:00.000-05:00,completeDate=2017-12-04T11:38:45.308-05:00,sequence=566], com.atlassian.greenhopper.service.sprint.Sprint@40469cbc[id=641,rapidViewId=247,state=CLOSED,name=LSA: Wk 2017/12/04-12/08,startDate=2017-12-04T11:39:44.474-05:00,endDate=2017-12-10T11:39:00.000-05:00,completeDate=2017-12-11T11:31:33.063-05:00,sequence=582], com.atlassian.greenhopper.service.sprint.Sprint@67ac099[id=642,rapidViewId=247,state=ACTIVE,name=LSA: Wk 2017/12/11-12/15,startDate=2017-12-11T11:38:27.821-05:00,endDate=2017-12-17T11:38:00.000-05:00,completeDate=<null>,sequence=601]]' with class 'java.util.ArrayList' to class 'java.lang.Double' at com.onresolve.scriptrunner.customfield.GroovyCustomField.getValueFromIssue(GroovyCustomField.groovy:291)
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.
Hi Eric,
The script that Daniel posted will return a List with the Sprints.
So in your case you will need the size of this list. So try with this one
import com.atlassian.greenhopper.service.sprint.Sprint
import com.atlassian.jira.component.ComponentAccessor
def cf = ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("Sprint")
def sprints = issue.getCustomFieldValue(cf) as List <Sprint>
sprints?.size()
And I suppose you have already configured the template and the searcher to be Number.
Please let us know how this script goes.
Regards, Thanos
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Thanos, I tried that and we are getting somewhree. When I look at the code checker for this I see this error.
[Static type checking] - Cannot return value of type fava.lang.Object on method returning type java.lan.double
line 6, column 12.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hey Eric,
I intentionally included type casting for the custom field's return value so you will not get this "false" alarm - is because of the Static Type Checking.
So I would not expect this to happen in the above script.
Can you please double check that you configured the scripted field with
Searcher: Number Searcher
Template: Number Field
Also did you try to preview it ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Yes the scripted field is using Number Searcher and Number Field.
When I preview I just noticed that for some projects I get that error during preview but others I do not.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Eric, this could be because some projects aren't agile maybe?
With such a small amount of information it's quite hard to try to diagnose your problem.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Daniel,
I'm sorry about not getting back to you. The project is Agile. What other information do you need? I just tried running it again and here is the error.
Time (on server): Mon Jan 15 2018 07:58:55 GMT-0600 (Central Standard Time)
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
2018-01-15 08:58:55,326 ERROR [customfield.GroovyCustomField]: ************************************************************************************* Script field failed on issue: WSA-1612, field: Sprint Count java.lang.Exception: The search indexer: class com.atlassian.jira.issue.customfields.searchers.ExactNumberSearcher expected your script to return a java.lang.Double, but it returned an java.util.ArrayList. We couldn't convert it to a java.lang.Double Caused by: org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object '[com.atlassian.greenhopper.service.sprint.Sprint@76b9b99e[id=716,rapidViewId=313,state=ACTIVE,name=WSA: Wk 2018/1/08-1/14,startDate=2018-01-08T08:52:09.835-05:00,endDate=2018-01-14T08:52:00.000-05:00,completeDate=<null>,sequence=716]]' with class 'java.util.ArrayList' to class 'java.lang.Double' at com.onresolve.scriptrunner.customfield.GroovyCustomField.getValueFromIssue(GroovyCustomField.groovy:293)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Daniel,
Could it be how we name our sprints that is causing this?
Eric
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'm having the same issue,
my current approach is to use a calculated custom field to calculate the amount of closed sprints the card has assiged.
an then use that custom field to determine how many times the card has been rolled over into the next sprint.
'the custom field grovy script will look something like.
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.greenhopper.service.sprint.Sprint
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def sprintFromIssue = customFieldManager.getCustomFieldObjectByName("Sprint")
def sprints = issue.getCustomFieldValue(sprintFromIssue)
def totalOE = 0
if (sprints) {
sprints.each{
if (it.state == Sprint.State.CLOSED){
totalOE += 1
}
}
return totalOE
}
then I will use that field in my jql - we'll see how it goes.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I think we did something similar a while back, albeit neither in groovy or relating to the sprint field - but what we did was to first ensure that any changes to the field were logged to the change history log and then when we needed to calculate the value we iterate across the change log counting the changes to the field ... for efficiency I seem to remember writing that value to the index.
I know that seems rather involved, but sometimes you have to work around Jira rather than with it!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have looked at that one in the past Katy but I was not able to get it working. I'm specifically wondering if someone can help with code for the number of sprints that an issue is in .
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
What code have you tried?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
It is deleted now, I am new to this and don't have experience with Groovy. When I put the code in the groovy window the coding checker flagged it with errors. So I opened a ticket with Adaptavist and they told me to create a post here.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Eric,
We would need to see the code you tried and the errors you are referencing, then we can figure out what may be wrong. Please post it here once you have it again.
Cheers,
Katy
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.
That's exactly what I need but they do not show the code, which is what I need help with.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Eric,
There is an example here: https://jamieechlin.atlassian.net/wiki/spaces/GRV/pages/1212429/Scripted+Fields?focusedCommentId=5046308#comment-5046308
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Eric,
There seems to be a similar question here: https://community.atlassian.com/t5/Jira-Software-questions/Issues-in-several-sprints/qaq-p/342596
Does that work for you?
Katy
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.