Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Jira Cloud Workflow condition using Scriptrunner

Davide Carta June 2, 2023

Hello

I need help generating a scriptrunner for jira cloud to apply to a workflow.

One of the transitions of this workflow (which is used by multiple projects) needs to have a condition or validator (I still need to figure it out).

All Stories and Tasks from the project PX that transition from Code Review to Released must have the custom field (Story Point) not empty.

If it is empty must return the error and ask to fallout that field.

PX is the only project that requires Story Point as a mandatory field, so the rest of the projects that use that workflow can transition without any restriction.

Can someone help me with this?

Thanks

4 answers

1 accepted

2 votes
Answer accepted
Bobby Bailey
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 2, 2023 edited

Hi @Davide Carta !

Just to clarify, if you want the user to attempt to make the transition and if it fails return an error message, you will want to create a ScriptRunner validator.

It is certainly possible to do what you need through ScriptRunner. @Graham Twine is correct that you must use a script if you have the workflow across multiple projects, but the script provided is for our DC/Server tool. With Cloud, you need to use Jira Expressions

The expression you want will look like this:

(project.key != "PX") || (issue.issueType.name != "Story" && issue.issueType.name != "Task") || (issue.customfield_10026 != null)



Just to break down what we are doing here, the Query checks to see if we are in project PX, if we are not, returns true and continues.

If we are in PX, return false and we move on to the next statement. The next statement checks to see if we are a Story or a Task, again, if we are not returns true, and moves on, otherwise we move to the next statement. 

By the time we have gotten to the last statement, we know that we are in PX, and dealing with a Story or a Task, so we check to see if the Custom Field is populated. If it is, continue, if not, fail the check. 

When you are writing these logic statements, you can see what information you can extract from the issue using this Jira Expressions Type Reference. Here you will see how I accessed the data and what else you can use. 

Hope this helps! Please let me know how you get on!

Bobby Bailey -- ScriptRunner Customer Success Manager 


Davide Carta June 2, 2023

hello @Bobby Bailey 

Thanks for the detailed answer.

The expression you provided worked as expected.

regards

Like • Bobby Bailey likes this
1 vote
Oliver Siebenmarck _Polymetis Apps_
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 2, 2023

Hi @Davide Carta ,

So, you want to use the same workflow in multiple projects and only in one project it should check for a value in a field. Also, you have ScriptRunner and you are on Jira Cloud.

The good news is, this is easily possible. The even better news is that you get to use Jira Expressions instead of (slightly) more complicated Groovy scripts. Here's how to do that:

Assuming that the Project key is PX and your customfield id is 10026, here's the whole code you need to add to the ScriptRunner condition/validator:

(project.key != "PX") || (issue.customfield_10026 != null)

The first part checks if you are running in the project PX. If you are, it evaluates to false and the second part is checked. If that customfield is empty (null), the transition gets blocked.

Hope that helps,
 Oliver

0 votes
Graham Twine
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 2, 2023

Hello @Davide Carta

 

A condition needs to be true in order to do something.

A validator must be true when doing something.

 

The something in this case is a transition in a workflow.

In the case of going somewhere in a car.

 

Conditions need to be met.

The driver is of legal driving age, has a valid driving license owns a car etc.

 

The validations that need to be met would be, the car has fuel, I have the keys, the battery is not flat.

 

We can AND and OR multiple conditions and validators if required.

This is a much better explanation in the context of Jira

 

Maybe in your case a validation is better because the transition is visible and when I click it the validation runs.

 

In the workflow transition, add a new  validation that is a scriptrunner custom validation.

Conditions and Validations need to return a true or false.

// Check what project this issue belongs to
// and return true if we are not interested in this
if (issue.projectObject.key != 'PX') return true

def isValidField = false

// Just confirm the field is populated
if (issue.customfield_xxxx.value) {
isValidField = true


// The field must have a specific value
} else if (issue.customfield_xxxx.value == 'some value') {
isValidField = true
}

return isValidField
0 votes
Sanjen Bariki
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 2, 2023

Hi Davide Carta,

 

Welcome to Atlassian Community!

As per your request, You can user Jira cloud in build feature to fulfil this requirement.

  • In PX project, Go to the required workflow then click on edit.
  • Now you can see the option to select Condition, Validation, Post-Function.
  • So we have to select the Validator option from the transition Code Review to Released.
  • In validator, Click on the Required Field then Save the Workflow.

Community Validator.png

 

Note :- Try it in UAT environment to, If works then Apply it on Prod.

Accept the answer if it helps

 

Regards,

Sanjen Bariki

Graham Twine
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 2, 2023

@Sanjen Barikithis workflow is used by many projects.

"One of the transitions of this workflow (which is used by multiple projects) "

 

I agree with your solution. It  is the simplest form of validation to confirm if a field has been populated but  in @Davide Cartacase it would fail for the other projects where this is not a mandatory field

Sanjen Bariki
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 2, 2023

Hi @Graham Twine ,

Agreed, But In @Davide Carta case he need to setup all the project where that workflow is used So I given the Simplest way to find the solution instead of using the Script. As you know script will be very difficult for the future preference.

 

Regards,

Sanjen Bariki

Davide Carta June 2, 2023

hello @Sanjen Bariki ,

thanks for the answer.

if I use the pre-set validator option, as you suggested, is it not going to be applied to all the projects using that workflow? 

only one project uses that field.

that is why I am thinking about the script

thanks

Sanjen Bariki
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
June 2, 2023

Hi @Davide Carta ,

 

Warm Greetings!

Below added the response inline.

  • if I use the pre-set validator option, as you suggested, is it not going to be applied to all the projects using that workflow? 

 

:- Yes, It will use for all the projects, Where that workflow is used.

Example:-

Project name ;- PX

Workflow Name :- Story and task Workflow But this workflow shared with 15 projects.

In the above case If you used the Validator in the Story and task workflow then the Custom field (Story Point)  validator will be Apply in all the 15 projects.

 

Note:- If you are changing anything in the Workflow then that will reflect in all the projects where that workflow is used.

 

Regards,

Sanjen Bariki

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
PREMIUM
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events