Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to check if sprint is active in behaviours

Mihai Mihai
Contributor
April 23, 2019

Hello,

 

Using Behaviours - from Scriptrunner, I would like to check if a sprint is active. If that is true, then I would like to make a certain field (Story Points) read-only.

 

Please let me know how to check if the sprint is active.

 

Thank you!

 

This is what I have so far:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.greenhopper.service.rapid.view.RapidViewService
import com.atlassian.greenhopper.service.sprint.Sprint


def SprintField = getFieldById("Sprint")
def StoryPointsField = getFieldById("Story Points")


if (SprintField.state == Sprint.State.ACTIVE) {

StoryPointsField.setReadOnly(true)

}

1 answer

1 accepted

1 vote
Answer accepted
PD Sheehan
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.
April 23, 2019

The "getFieldById" method from Behaviors returns a "formField" object. This is a scriptrunner/behavior class I think. Not a native class from Jira.

And the value stored in the formField is probably just the sprint Id.

So you'll need to get a customField object then lookup the value from the underlying issue.

import com.atlassian.jira.component.ComponentAccessor

def customFieldManager = ComponentAccessor.customFieldManager

def sprintFormField = getFieldByName("Sprint")
def storyPtFormField = getFieldByName("Story Points")

def sprintCustField = customFieldManager.getCustomFieldObject(sprintFormField.fieldId)
sprint = issueContext.getCustomFieldValue(sprintCustField)

if(sprint && sprint.state == "ACTIVE"){
storyPtFormField.setReadOnly(true)
} else {
storyPtFormField.setReadOnly(false)
}

If you need to react to newly selected sprint values, then you'll need to fetch those details using the SprintManager.

Something along those lines may work:

import com.atlassian.greenhopper.service.sprint.SprintManager
import com.onresolve.scriptrunner.runner.customisers.JiraAgileBean
@JiraAgileBean
SprintManager sprintManager

def sprintFormField = getFieldByName("Sprint")
def storyPtFormField = getFieldByName("Story Points")
storyPtFormField.setReadOnly(false)

if(sprintFormField.value){
def sprint = sprintManager.getSprint(sprintFormField.value as Long)
if(sprint.isValid() && sprint.value.state == "ACTIVE" ){
storyPtFormField.setReadOnly(true)
}
}
Mihai Mihai
Contributor
April 24, 2019

Thank you very much, this works great!

Vineela Durbha
Contributor
November 2, 2023

Hi @PD Sheehan 

I have tried both the above scripts and i see errors in the script..

Also i see null is being returned from getFieldByName("Sprint")

It says 'No such property: state for class: java.lang.Object

'No such property: value for class: java.lang.Object

 

Is there something i am missing as i am not execute above code..

Can you help me on this

Suggest an answer

Log in or Sign up to answer