Change statut based on Component on create

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

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.

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

 

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.

but how to set the next status ???

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.

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 ?

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.

 

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 

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()

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)

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

it work :D :D

thank you 

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Monday in Agility Beta

We've moved!

A note to all watchers that we've moved to a new community home... https://community.atlassian.com/t5/Agility/ct-p/agility Please update your notification subscriptions to keep across the ...

61 views 0 2
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you