Post Script to do a Workflow transition or assign Status

Manuel Aranda April 30, 2019

Hi all,

I have a transition that provokes to create a subtak, and inside the subtask I am trying to put a Post Script function to provoke the new subtask to move State automatically.


I have tried in two different ways trying a workflow transition or setting the status by id but I cannot achive it anyway.

My code is:

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.bc.issue.IssueService
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.atlassian.jira.issue.IssueInputParameters

def iip = IssueInputParameters.newInstance()
iip.setStatusId("19")

ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
IssueService issueService = ComponentAccessor.getIssueService()
issueService.validateUpdate(user, 19, iip)

 

but it doesn't work. The status I want to move has id ="19" but it is also permitted by workflow but I cannot do the transition neither,

Any help will be highly appreciated!

Thanks in advance,

Kind Regards,

Manuel.

1 answer

0 votes
Zachary Miller May 1, 2019

Hi @Manuel Aranda 

I am not sure your script will work for transitioning issues. Based off of https://community.atlassian.com/t5/Marketplace-Apps-questions/ScriptRunner-how-to-change-status-of-an-Issue/qaq-p/628842

The script below should work for you

____________________

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParametersImpl

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueManager = ComponentAccessor.issueManager
def issue = issueManager.getIssueObject(event.issue.key)
IssueService issueService = ComponentAccessor.getIssueService()

def actionId = <Replace with transition id>
def transitionValidationResult
def transitionResult
def customFieldManager = ComponentAccessor.getCustomFieldManager()

transitionValidationResult = issueService.validateTransition(currentUser, issue.id, actionId,new IssueInputParametersImpl())
if (transitionValidationResult.isValid()) {
transitionResult = issueService.transition(currentUser, transitionValidationResult)
}

____________________

NOTE: I added this as a script listener, but this should work as a post function as well

Manuel Aranda May 6, 2019

Hi @Zachary Miller ,


I have tested this script as a post-function and I it doesn't work for me. It compiles with no errors, but it fails in execution and I don't know why.

Also when I create the subtask I configured the transition as a Subtask Action and it doesn't work neither:

image.png

Thanks for your help in advance!

 

Kind Regards,

Manuel.

Zachary Miller May 6, 2019

Hi @Manuel Aranda 

I may have not phrased myself correctly. Try adding that script as a separate post function after the subtask is created. I think the issue here is that the sub task is trying the be transitioned before it is created.

Manuel Aranda May 6, 2019

Hi @Zachary Miller ,

 

I have tried to put the code in a new Post Script function after subtask is created but it continue failing. The problem now is getting the subtask as this Post Function is runned from parent right?

Ths is the code I tried:

(321 Transition is("Subtarea Diseño") from "DPTO. INFORMÁTICA" status to "PROCESANDO DISEÑO" status)

 

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParametersImpl
import com.atlassian.jira.config.SubTaskManager
import com.onresolve.scriptrunner.runner.util.UserMessageUtil

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueManager = ComponentAccessor.issueManager
def issue = issueManager.getIssueObject(event.issue.key)
IssueService issueService = ComponentAccessor.getIssueService()

def actionId = 321
def transitionValidationResult
def transitionResult
def customFieldManager = ComponentAccessor.getCustomFieldManager()

transitionValidationResult = issueService.validateTransition(currentUser, issue.id, actionId,new IssueInputParametersImpl())
//if (transitionValidationResult.isValid()) {
//transitionResult = issueService.transition(currentUser, transitionValidationResult)
//}

Collection subTasks = issue.getSubTaskObjects();

for(subtask in subTasks) {
String status = subtask.getStatus().getName();
UserMessageUtil.success('El estado de la tarea es ' + status)
if(status == "DPTO. INFORMÁTICA") //also tried with "Resolved"
{
transitionResult = issueService.transition(currentUser, transitionValidationResult)
}
}

 

Thanks in advance for your help!

Zachary Miller May 6, 2019

@Manuel Aranda 

I think you are correct. Thank was my mistake.

Replace

"issue = event.issue"

to 

"def issue= event.issue.getSubTaskObjects()"

I think that should fix the problem

Manuel Aranda May 6, 2019

@Zachary Miller 

 

Thanks so much for your help Zachary, you are so kind.

It continue failing in execution. I modified the script with your latest comment,

The code:

 

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParametersImpl
import com.atlassian.jira.config.SubTaskManager
import com.onresolve.scriptrunner.runner.util.UserMessageUtil

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueManager = ComponentAccessor.issueManager
def issue = event.issue.getSubTaskObjects()
IssueService issueService = ComponentAccessor.getIssueService()

def actionId = 321
def transitionValidationResult
def transitionResult
def customFieldManager = ComponentAccessor.getCustomFieldManager()
for(subtask in issue) {

transitionValidationResult = issueService.validateTransition(currentUser, subtask.id, actionId,new IssueInputParametersImpl())

String status = subtask.getStatus().getName();
UserMessageUtil.success('El estado de la tarea es ' + status)
if(status == "DPTO. INFORMÁTICA")
{
transitionResult = issueService.transition(currentUser, transitionValidationResult)
}
}

Manuel Aranda May 6, 2019

It doesn't show up the message:

 

UserMessageUtil.success('El estado de la tarea es ' + status)

Manuel Aranda May 6, 2019

@Zachary Miller 

 

Hi again, I have tried to put a message before "def issue = event.issue.getSubTaskObjects()" and it shows up, but if I put the message after "def issue = event.issue.getSubTaskObjects()" it doesn't show up

Manuel Aranda May 6, 2019

@Zachary Miller 

I have tried this code:

def subtasks = issue.getSubTaskObjects()
UserMessageUtil.success('TESTSSS ' + subtasks.getProperties())

and it shows up as substasks is java.util.ArrayList Empty:true

image.png

It seems the subtasks are empty because they don't get the substask object correctly

Zachary Miller May 6, 2019

@Manuel Aranda 

I went ahead and implemented this and was able to get it to work

1.PNGThese were added to the parent issues workflow. The script post function has the code below

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParametersImpl

def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueManager = ComponentAccessor.issueManager
def subTasks = issue.getSubTaskObjects()
def subTask = subTasks[0]
IssueService issueService = ComponentAccessor.getIssueService()
if (subTask != null){
def actionId = <transition id of the sub task>
def transitionValidationResult
def transitionResult
def customFieldManager = ComponentAccessor.getCustomFieldManager()

transitionValidationResult = issueService.validateTransition(currentUser, subTask.id, actionId,new IssueInputParametersImpl())
if (transitionValidationResult.isValid()) {
transitionResult = issueService.transition(currentUser, transitionValidationResult)
}
}

Please let me know if this throws any errors

Manuel Aranda May 6, 2019

@Zachary Miller 

 

Hi again Zachary,

I have tried your last code and it doesn't work. I have tried several times different scripts and the last version I have is this one:

import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParametersImpl
import com.atlassian.jira.config.SubTaskManager
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.atlassian.jira.workflow.WorkflowTransitionUtil;
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl;
import com.atlassian.jira.util.JiraUtils;

String currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueManager = ComponentAccessor.issueManager
def subTaskManager = ComponentAccessor.getSubTaskManager()
def subtasks = subTaskManager.getSubTaskObjects(issue)
UserMessageUtil.success('Subtareas creadas ' + subtasks.size())
IssueService issueService = ComponentAccessor.getIssueService()

def actionId = 321
def transitionValidationResult
def transitionResult
def customFieldManager = ComponentAccessor.getCustomFieldManager()
WorkflowTransitionUtil workflowTransitionUtil = ( WorkflowTransitionUtil ) JiraUtils.loadComponent( WorkflowTransitionUtilImpl.class );

System.sleep(2)

for(subtask in subtasks) {
UserMessageUtil.success('FOR ' + subtask.getKey())
//transitionValidationResult = issueService.validateTransition(currentUser, subtask.id, actionId, new IssueInputParametersImpl())
MutableIssue mutableIssue = (MutableIssue) subtask;
workflowTransitionUtil.setIssue(mutableIssue)
workflowTransitionUtil.setUserkey(currentUser)
workflowTransitionUtil.setAction(321)
workflowTransitionUtil.validate()
workflowTransitionUtil.progress()

String status = subtask.getStatus().getName();
if(status == "DPTO. INFORMÁTICA")
{
//transitionResult = issueService.transition(currentUser, transitionValidationResult)
UserMessageUtil.success('Transición de ' + subtask.getKey())
}
}

It shows ('FOR ' + subtask.getKey()) only once when there are 2 subtask, so the error come with the MutableIssue or the workflowTransitionUtil, if I only put the UserMessageUtil.success it show 2 subtask so the for is doing correct. I also comment the lines "transitionResult" as they either work,

Thanks in advance,

Manuel.

Manuel Aranda May 7, 2019

@Zachary Miller 

Hi again Zachary,

I have just tries with a try catch loop in order to print the error, and I have checked that the error shows up in the workflowTransitionUtil.progress() and the errors says:

 

Error: User null doesn't have permission transition for issue TEL-215.

How I get user null? How can I get a correct user to do the transition?

Thanks in advance!

Manuel Aranda May 7, 2019

@Zachary Miller 

Hi again,

Finally I could succes with the transition, and I state my last problem here maybe you can help. If I configure a Condition in the transition to be hidden for the users view then I got Error on the transition due to forbidden action, but If I delete the Condition of hidden transition then it works perfectly, I put the error here:ç

Translation: It seems you tried to do a ilegal action in the workflow. If you think this message is incorrect contact Administrators,

image.png

Manuel Aranda May 7, 2019

@Zachary Miller 

Okay, I have made a workaround, I put a scripted Condition:

issue.getIssueType().name == "Subtask"


And it also works for the Task ticket, the transition is hidden as the Condition blocks it, so it is what I expected,

Thanks in advance,

I will close the ticket,
Manuel.

Suggest an answer

Log in or Sign up to answer