Scripted Post Function using Scriptrunner: Trying to automatically perform a second transition Edited

Hi,

After a transition, I want the script to look to see if another ticket is resolved. If the ticket is resolved, it will perform the next transition. I have written a postfunction to look for the resolved ticket and it is found. But for some reason, it still doesn't perform the transition. I even put some debugger logs to make sure that the script is performing as it should. I believe my problem lies in either my issueService.validateTransition() function or maybe the order of when the post function has been executed. I have tried to make this postfunction run as the second, third, forth or last postfunction and that hasn't worker. I have also checked the permissions and I am able to transition the issue. My script can be seen below:

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.web.bean.PagerFilter;
import java.lang.Object
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import org.apache.log4j.Logger
import org.apache.log4j.Level

//create debugger
def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)

String projectName = issue.getProjectObject().name

boolean check = 0;
def issueService = ComponentAccessor.getIssueService()
def issueInputParameters = issueService.newIssueInputParameters()
issueInputParameters.with {}

String jql = 'project = ' + projectName + ' AND status = Resolved';
log.debug("Project = " + projectName)

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
log.debug("User = " + user.getName())

SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
SearchService.ParseResult parseResult = searchService.parseQuery(user, jql)

def issuesList;

if(parseResult.isValid()){
issuesList = (searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter()).getIssues())
log.debug("Valid Parse")
}
else{
check = 1;
}

if(issuesList.size() >= 0){
for (int i =0; i<issuesList.size();i++){
if(issuesList.get(i).summary == "Marketing BLOG"){
log.debug("Found the Marketing BLOG ticket")
def validationResult = issueService.validateTransition(user, issue.getId(), 21, issueInputParameters)
def issueResult = issueService.transition(user, validationResult)

}
}
}

 

Also, my log results can be seen below:

2017-08-21 10:20:15,730 DEBUG [acme.CreateSubtask]: Project = Test
2017-08-21 10:20:15,730 DEBUG [acme.CreateSubtask]: Project = Test
2017-08-21 10:20:15,730 DEBUG [acme.CreateSubtask]: User = alonardelli
2017-08-21 10:20:15,730 DEBUG [acme.CreateSubtask]: User = alonardelli
2017-08-21 10:20:15,773 DEBUG [acme.CreateSubtask]: Valid Parse
2017-08-21 10:20:15,773 DEBUG [acme.CreateSubtask]: Valid Parse
2017-08-21 10:20:15,773 DEBUG [acme.CreateSubtask]: Found the Marketing BLOG ticket
2017-08-21 10:20:15,773 DEBUG [acme.CreateSubtask]: Found the Marketing BLOG ticket

 

1 answer

I would try two things:

1) use the isValid() method to validate the transition

2) get the error collection in case the transition fails

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.CustomFieldManager;
import com.atlassian.jira.issue.fields.CustomField;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.crowd.embedded.api.User;
import com.atlassian.jira.user.ApplicationUser;
import com.atlassian.jira.bc.issue.search.SearchService;
import com.atlassian.jira.web.bean.PagerFilter;
import java.lang.Object
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import org.apache.log4j.Logger
import org.apache.log4j.Level

//create debugger
def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)

String projectName = issue.getProjectObject().name

boolean check = 0;
def issueService = ComponentAccessor.getIssueService()
def issueInputParameters = issueService.newIssueInputParameters()
issueInputParameters.with {}

String jql = 'project = ' + projectName + ' AND status = Resolved';
log.debug("Project = " + projectName)

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
log.debug("User = " + user.getName())

SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
SearchService.ParseResult parseResult = searchService.parseQuery(user, jql)

def issuesList;

if(parseResult.isValid()){
issuesList = (searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter()).getIssues())
log.debug("Valid Parse")
}
else{
check = 1;
}

if(issuesList.size() >= 0){
for (int i =0; i<issuesList.size();i++){
if(issuesList.get(i).summary == "Marketing BLOG"){
log.debug("Found the Marketing BLOG ticket")
def validationResult = issueService.validateTransition(user, issue.getId(), 21, issueInputParameters)
if (validationResult.isValid()) {
def transitionResult = issueService.transition(user, validationResult)
if (transitionResult.isValid()) {
log.debug("Transitioned issue ${issue.key}")
}
else {
log.debug("Transition result is not valid")
log.debug(transitionResult.getErrorCollection())
}
}
else {
log.debug("The transitionValidation is not valid")
log.debug(validationResult.getErrorCollection())
}
}
}
}

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Tuesday in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

112 views 0 3
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you