Come for the products,
stay for the community

The Atlassian Community can help you and your team get more value out of Atlassian products and practices.

Atlassian Community about banner
4,299,960
Community Members
 
Community Events
165
Community Groups

Help with script of Transition Linked issues with scriptrunner

Edited

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!

1 answer

0 votes

"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.

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!

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 Vikrant Yadav likes this

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

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Published in Confluence

An update on Confluence Cloud customer feedback – June 2022

Hi everyone, We’re always looking at how to improve Confluence and customer feedback plays an important role in making sure we're investing in the areas that will bring the most value to the most c...

330 views 2 7
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you