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

This widget could not be displayed.

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
Community showcase
Published Aug 22, 2018 in Marketplace Apps

How a Marketplace app tech team is achieving gender diversity

Hello! My name is Genevieve Blanch, and I'm the Marketing Manager at RefinedWiki, creators of apps to give teams the tools to customize Atlassian platforms. Currently, 44% of the tech team at Re...

533 views 3 19
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