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,
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
That's exactly what I need but they do not show the code, which is what I need help with.
There is an example here: https://jamieechlin.atlassian.net/wiki/spaces/GRV/pages/1212429/Scripted+Fields?focusedCommentId=5046308#comment-5046308
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 .
What code have you tried?
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.
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,
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!
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.ComponentAccessordef 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)
Cancel
Daniel,
Any ideas on how I can fix those errors?
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.Sprintimport com.atlassian.jira.component.ComponentAccessordef 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
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.ComponentAccessorimport com.atlassian.jira.issue.MutableIssueimport com.atlassian.jira.issue.Issueimport com.atlassian.jira.issue.IssueManagerimport com.atlassian.jira.event.type.EventDispatchOptionimport com.atlassian.jira.user.ApplicationUserimport com.atlassian.greenhopper.service.sprint.Sprintimport 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.
It looks like you're new here. Sign in or register to get started.