Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
Celebration

Earn badges and make progress

You're on your way to the next level! Join the Kudos program to earn points and save your progress.

Deleted user Avatar
Deleted user

Level 1: Seed

25 / 150 points

Next: Root

Avatar

1 badge earned

Collect

Participate in fun challenges

Challenges come and go, but your rewards stay with you. Do more to earn more!

Challenges
Coins

Gift kudos to your peers

What goes around comes around! Share the love by gifting kudos to your peers.

Recognition
Ribbon

Rise up in the ranks

Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!

Leaderboard

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,559,696
Community Members
 
Community Events
185
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

1 vote
Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Jul 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.

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!

Peter-Dave Sheehan
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
Sep 03, 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 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
AUG Leaders

Atlassian Community Events