Forums

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

Help with script of Transition Linked issues with scriptrunner

Laura Carbonell July 15, 2021

Hi!. 

I'm trying to create a script with scriptrunner to be able to tansition two linked issues that, each from different projects. What I want is that when one of these issues goes to "done", the other one does it automatically. 

¿Does anyone have an example of a script i can use?

¿Do i have to select a postfunction or a condition in scriptrunner?

I'm new in groovy and I'm lost!

Thanks!

2 answers

Suggest an answer

Log in or Sign up to answer
2 votes
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.
July 15, 2021

"Condition" in workflow only determine if/when a transition button should be available.

PostFunction happens after the transition has been initiated and passed all the validation rules (validators).

So your scenario is a good fit for a post function.

But you indicated that the 2 linked issues are in different projects. That means that they may also have different workflows. That means that you may need to create a post function in both workflows if either of the 2 issues could be marked done first.

You didn't specify your deployment type... so I'll assume "Server". If you are on Cloud, someone else will need to help you as I don't work with Cloud much.

In the workflow Post Functions tab, click "Add post function"

Select "Custom script post-function"

Your script would look something like this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParametersImpl
import com.atlassian.jira.workflow.TransitionOptions

def issueService = ComponentAccessor.issueService
def issueLinkManager = ComponentAccessor.issueLinkManager
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

//if you have 2 different workflows use the following line
//def actionId = 21 //put the ID of the transition you want to run, that's the number that shows in the workflow text mode for the transition that ends in "Done"

//if the same workflow on all linked issues use this line
def actionId = transientVars["actionId"] as Integer

def links = issueLinkManager.getLinkCollection(issue, currentUser)
links.allIssues.each{linkedIssue->
def transitionOptions = new TransitionOptions.Builder()
//if you want to ignore any condition that would normally hide the transition, uncoment the next line
//transitionOptions =transitionOptions.skipConditions()
//if you want to ignore any validator that could stop the transition, uncoment the next line
//transitionOptions =transitionOptions.skipValidators()
transitionOptions = transitionOptions.build()
def transitionValidationResult = issueService.validateTransition(currentUser, linkedIssue.id, actionId, new IssueInputParametersImpl(), transitionOptions)
if(transitionValidationResult.isValid()){
def transitionResult = issueService.transition(currentUser, transitionValidationResult)
}
}

If you are likely to have other issue links, this might need to be adjusted a little.

Laura Carbonell September 3, 2021

Hi Peter!, 

Thank you very much for your reply. Unfortunately it didn't work out

I've used the script that you mention of a single workflow, and in principle tells me that it is ok, and that it has been executed without any error: 



import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParametersImpl
import com.atlassian.jira.workflow.TransitionOptions

def issueService = ComponentAccessor.issueService
def issueLinkManager = ComponentAccessor.issueLinkManager
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def actionId = transientVars["actionId"] as Integer

def links = issueLinkManager.getLinkCollection(issue, currentUser)
links.allIssues.each{linkedIssue->
def transitionOptions = new TransitionOptions.Builder()
transitionOptions = transitionOptions.build()
def transitionValidationResult = issueService.validateTransition(currentUser, linkedIssue.id, actionId, new IssueInputParametersImpl(), transitionOptions)
if(transitionValidationResult.isValid()){
def transitionResult = issueService.transition(currentUser, transitionValidationResult)
}
}

But when I move the "main" issue to done (which is the transition where I want the script to run) the secondary issue (which is the one that is linked) does not change state also to done, it stays where it was, do you know why it could be?. 

Thank you very much and sorry for taking so long to reply!

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.
September 3, 2021

Let's try to add some debug messaging in your script.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueInputParametersImpl
import com.atlassian.jira.workflow.TransitionOptions

def issueService = ComponentAccessor.issueService
def issueLinkManager = ComponentAccessor.issueLinkManager
def currentUser = ComponentAccessor.jiraAuthenticationContext.loggedInUser

def actionId = transientVars["actionId"] as Integer

def links = issueLinkManager.getLinkCollection(issue, currentUser)
log.info "Found ${links.allIssues.size()} links in $issue.key: ${links.allIssues*.key}"
links.allIssues.each{linkedIssue->

log.info "Processing linked issue: $linkedIssue.key"
def transitionOptions = new TransitionOptions.Builder()

transitionOptions = transitionOptions.build()
def transitionValidationResult = issueService.validateTransition(currentUser, linkedIssue.id, actionId, new IssueInputParametersImpl(), transitionOptions)
if(transitionValidationResult.isValid()){
def transitionResult = issueService.transition(currentUser, transitionValidationResult)
} else {
log.error "Unable to transition $linkedIssue.key because of an error: transitionValidationResult.errorCollection.errorMessages"
}

}

After you execute the transition on your main issue, go back to the transition edit screen, you will be able to access some logs for the script and get some clues as to why it isn't working.

One possibility is that "actionId" is not a valid action for your linked issues (that's the id for the main issue).

You may need to hardcode the id of the transition you want to perform on those issues. The ID you want is the number in the parenthesis next to the transition name in the text view of the workflow.

Like # people like this
Laura Carbonell September 13, 2021

Hi Peter!, 

Thanks you very much, I have changed "actionid" by the number and I have used this last script and now it works!!

Thank you very much for your help

Steve Long -Evelon-
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.
September 3, 2024

@PD Sheehan I wonder if you would be able to advise on how to amend this script, to only transition issues of a certain link type? 

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.
September 3, 2024

@Steve Long -Evelon- 

Replace:

links.allIssues.each{linkedIssue->

with

links.getOutwardIssues('your link type').each{linkedIssue->

or 

links.getInwardIssues('your link type').each{linkedIssue->
Like Vikrant Yadav likes this
0 votes
Hari Gadamsetty
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
September 14, 2024

I have similar requirement and I'm using jira cloud is there any possibility ?

TAGS
AUG Leaders

Atlassian Community Events