Forums

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

How to transition issue when Sprint field value is changed to concrete one?

Анна Рыбина September 14, 2021

I'm trying to write a Listener in Scriptrunner JIRA Server

It does a fast-track transition of an issue when a user inputs an issue to the concrete sprint

Event: Issue Updated

Condition:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.greenhopper.service.rapid.view.RapidViewService
import com.atlassian.greenhopper.service.sprint.SprintIssueService
import com.atlassian.greenhopper.service.sprint.SprintManager
import com.atlassian.greenhopper.service.sprint.SprintService
import org.apache.log4j.Logger
import org.apache.log4j.Level

def customFieldManager = ComponentAccessor.getCustomFieldManager();

def sprintField = customFieldManager.getCustomFieldObjectByName("Sprint");
def sprints = event.issue.getCustomFieldValue(sprintField);

sprints?.id?.equals(1837);

 

Action: transition to status

 

I've tried several variants, but it doesn't work. I think that the problem is in this line: 

sprints?.id?.equals(1837);

1 answer

1 accepted

2 votes
Answer accepted
Leo
Community Champion
September 14, 2021

Hi @Анна Рыбина,

if the issue is passed from one to another sprint it has more than one value and it'll be returned as List

I'm not quite sure to simplify the condition but below snippet is working fine in my sandbox

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.greenhopper.service.sprint.Sprint

def sprints = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Sprint")[0])
def isSprint = false
sprints.each{
Sprint s = it as Sprint
if(s.id == 835){
isSprint = true
}
}

return isSprint

hope this helps

 

BR,

Leo

Анна Рыбина September 14, 2021

Dear Leo, thank you very very much! It works!!! 

Анна Рыбина September 14, 2021

@Leo But maybe you can help me or advice anything for similar problem, but when Sprint field is empty

I'm trying to transition an issue to status called "Backlog" when an issue is moved to backlog

But nothing like comparing Sprint value field with null works, even with the help of your code above

Leo
Community Champion
September 14, 2021

@Анна Рыбина, it is quite easy only. 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.greenhopper.service.sprint.Sprint

def sprints = issue.getCustomFieldValue(ComponentAccessor.getCustomFieldManager().getCustomFieldObjectsByName("Sprint")[0])
if(sprints){
return false // sprint has some value
}else{
return true // sprint field is empty/null
}

Note: this will only work if the issue is assigned to single sprint and move to backlog.
if the issue is transferred from one to another sprint this condition will fail

 

BR,

Leo

Анна Рыбина September 15, 2021

@Leo Thank you very much!!! 

Suggest an answer

Log in or Sign up to answer