Change statut based on Component on create

taher shili June 9, 2017

I need to change statut based on component choose . on create i want to verify the component choose and set the next statut 

1 answer

Suggest an answer

Log in or Sign up to answer
1 vote
Thomas Venekamp June 9, 2017

Hello,

you find the answer here:

https://community.atlassian.com/t5/Answers-Developer-Questions/Scriptrunner-transition-next-step/qaq-p/517722#M55378

You can only set the next status, if there is a valid transition step.

taher shili June 9, 2017

I need to verify component choose and before that change the status to Open Or Need approval 
how can i do it ?

 

Thomas Venekamp June 9, 2017

You get the current components with:

currComponents = issue.getComponents()

Then you can build a loop to check if your desired components would have been chosen:

for (c in currentComponents) {
 if (c.getName() == component) {
  DoSomething()
 }
}

There you can perform the desired workflow transition and then use break to leave the loop. The following logic depends on your needs.

You can find the JIRA API for your version here:

https://docs.atlassian.com/jira/

There you find all classes and methods. Valuable information on the script runner can be found here: https://scriptrunner.adaptavist.com/latest/jira/quickstart.html

I hope you can get your code together. If not, feel free to ask again.

taher shili June 9, 2017

but how to set the next status ???

Thomas Venekamp June 9, 2017

This is the information i got from the link i posted in the original answer above:

import com.atlassian.jira.workflow.WorkflowTransitionUtil
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl
import com.atlassian.jira.util.JiraUtils
 
// transition an issue
def workflowTransitionUtil = ( WorkflowTransitionUtil ) JiraUtils.loadComponent( WorkflowTransitionUtilImpl.class )
workflowTransitionUtil.setIssue(issue)
workflowTransitionUtil.setUserkey(userKey)
workflowTransitionUtil.setAction(workflow action id)
 
// validate and transition issue
workflowTransitionUtil.validate()
workflowTransitionUtil.progress()

I tested it in our development environment and it worked.

The userKey is a String. This is the user performing the transition.

The workflow action id is an Integer. You get the action id through your workflow. When you edit the workflow in the admin section, you can see the Ids behind the workflow steps in the text mode.

taher shili June 9, 2017

the probleme is i need a verification on create issue If component = " bla bla bla " then status =" open" else status ="Inprogress" 

do you inderstand me ?

Thomas Venekamp June 9, 2017

Okay, i will go through the steps you need to do, to solve your problem.

I have two ideas for you.

1. Create a "dummy" status after the create transition with two outgoing statuses to open and in progress.  In the post-function you can put a code to test the components and then take one or the other workflow transition to get to open or in progress.

2. The create transition goes directly into open and you change your status with a post-function only if a certain component has been taken. You can restrict the transition to the in progress status so that the users in open can't see the workflow transition.

The next code uses the first idea. But I think you can adapt to the second idea, if you prefer that.

Put the code in the create workflow transition after the first step Creates the issue originally.  

If your code is on top, you can't access the issue and therefore you can't check, if a certain component has been taken.

 

import com.atlassian.jira.workflow.WorkflowTransitionUtil
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl
import com.atlassian.jira.util.JiraUtils

def currentComponents = issue.getComponents()
 
def workflowTransitionUtil = ( WorkflowTransitionUtil ) JiraUtils.loadComponent( WorkflowTransitionUtilImpl.class )
workflowTransitionUtil.setIssue(issue)
workflowTransitionUtil.setUserkey(userKey)

for (c in currentComponents) {
 if (c.getName() == "Option 1") {
  workflowTransitionUtil.setAction(10)
 }
 if (c.getName() == "Option 2") {
  workflowTransitionUtil.setAction(12)
 }
}
 
// validate and transition issue
workflowTransitionUtil.validate()
workflowTransitionUtil.progress()

I haven't tested this exact code, but you can see the idea behind it. You need to enter your values for the userKey and the action ids and you need to check, if this logic fits your needs, because an issue can have more then one component. I don't know how you want to react then.

I hope this is a good start to solve your problem.

 

taher shili June 9, 2017

i choose the first idea with a Verify transaction in after create issue and i'm put this code 

import com.atlassian.jira.workflow.WorkflowTransitionUtil
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl
import com.atlassian.jira.util.JiraUtils

 def currentComponents = issue.getComponents()
 
def workflowTransitionUtil = ( WorkflowTransitionUtil ) JiraUtils.loadComponent( WorkflowTransitionUtilImpl.class )
workflowTransitionUtil.setIssue(issue)
workflowTransitionUtil.setUserkey(currentUser.name)

for (c in currentComponents) {
 if (c.getName() == "NEW URL") {
  workflowTransitionUtil.setAction(191)
 }
 else {
  workflowTransitionUtil.setAction(181)
 }
}
 
workflowTransitionUtil.validate()
workflowTransitionUtil.progress()

But it don't work Thank's to help me 

Thomas Venekamp June 11, 2017

Good morning,

I think you get the following error:

groovy.lang.MissingPropertyException: No such property: currentUser

You have to add some lines to your code to get the currentUser.

import com.atlassian.jira.component.ComponentAccessor

authenticationContext = ComponentAccessor.getJiraAuthenticationContext()
currentUser = authenticationContext.getLoggedInUser().getName()
taher shili June 12, 2017

i got this error 

Time (on server): Mon Jun 12 2017 09:02:16 GMT+0100 (Afr. centrale Ouest)
The following log information was produced by this execution. Use statements like:log.info("...") to record logging information.
2017-06-12 10:02:16,411 ERROR [workflow.ScriptWorkflowFunction]: *************************************************************************************
2017-06-12 10:02:16,427 ERROR [workflow.ScriptWorkflowFunction]: Script function failed on issue: null, actionId: 1, file: <inline script>
java.lang.IllegalArgumentException: No workflow action with id '191' available for issue null
 at com.atlassian.jira.workflow.WorkflowTransitionUtilImpl.retrieveActionDescriptor(WorkflowTransitionUtilImpl.java:162)
 at com.atlassian.jira.workflow.WorkflowTransitionUtilImpl.retrieveActionDescriptorWithPermissionCheck(WorkflowTransitionUtilImpl.java:146)
 at com.atlassian.jira.workflow.WorkflowTransitionUtilImpl.getActionDescriptor(WorkflowTransitionUtilImpl.java:133)
 at com.atlassian.jira.workflow.WorkflowTransitionUtilImpl.getFieldScreenRenderer(WorkflowTransitionUtilImpl.java:292)
 at com.atlassian.jira.workflow.WorkflowTransitionUtilImpl.validateFieldsParams(WorkflowTransitionUtilImpl.java:255)
 at com.atlassian.jira.workflow.WorkflowTransitionUtilImpl.access$500(WorkflowTransitionUtilImpl.java:37)
 at com.atlassian.jira.workflow.WorkflowTransitionUtilImpl$2.get(WorkflowTransitionUtilImpl.java:247)
 at com.atlassian.jira.workflow.WorkflowTransitionUtilImpl$2.get(WorkflowTransitionUtilImpl.java:236)
 at com.atlassian.jira.workflow.WorkflowTransitionUtilImpl.impersonateUser(WorkflowTransitionUtilImpl.java:380)
 at com.atlassian.jira.workflow.WorkflowTransitionUtilImpl.validate(WorkflowTransitionUtilImpl.java:236)
 at com.atlassian.jira.workflow.WorkflowTransitionUtil$validate$2.call(Unknown Source)
 at Script3708.run(Script3708.groovy:23)
Thomas Venekamp June 12, 2017

I built a new workflow and tested all my suggested ideas and it didn't work. In all other workflow steps I can easily manipulate the outcome, but I can't get it to work in the create-transition.

But don't worry, I have a new idea. I don't know what version of the script runner you use. We use 4.3.16 and it has a built-in Fast-track transition script.

Edit the workflow. Go to the create transition and there to the post-function section. Then add a new post-function. Choose Script Post-Function and then Fast-track transition an issue.

As Condition fill in the following:

 

def currentComponents = issue.getComponents()

for (c in currentComponents) {
 if (c.getName() == "NEW URL") {
        return true
    }    
}
return false

and as Action choose the workflow step 191.

 

Confirm your inputs with Add. Now you have to move the post-function to the bottom - under the Fire Event function.

You have to repeat the above steps for the other workflow step, but now use the following Condition

 

def currentComponents = issue.getComponents()

for (c in currentComponents) {
 if (c.getName() == "NEW URL") {
        return false
    }    
}
return true

and as Action choose the workflow step 181.

 

Now confirm and move the function down next to the other fast-track transition function.

I hope this works for you. If not, feel free to ask again.

Source: https://scriptrunner.adaptavist.com/4.3.6/jira/builtin-scripts.html#_fast_track_transition_an_issue

taher shili June 14, 2017

it work :D :D

thank you 

TAGS
AUG Leaders

Atlassian Community Events