You're on your way to the next level! Join the Kudos program to earn points and save your progress.
Level 1: Seed
25 / 150 points
Next: Root
1 badge earned
Challenges come and go, but your rewards stay with you. Do more to earn more!
What goes around comes around! Share the love by gifting kudos to your peers.
Keep earning points to reach the top of the leaderboard. It resets every quarter so you always have a chance!
Join now to unlock these features and more
The Atlassian Community can help you and your team get more value out of Atlassian products and practices.
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!
"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!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.