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.
@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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Replace:
links.allIssues.each{linkedIssue->
with
links.getOutwardIssues('your link type').each{linkedIssue->
or
links.getInwardIssues('your link type').each{linkedIssue->
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I have similar requirement and I'm using jira cloud is there any possibility ?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.