ScriptRunner - Need help creating subtasks based on conditions. (Create a Subtask Postfunction)

Michael
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 5, 2023

Hi all,

I'm trying to use the "Create a sub-task [ScriptRunner]" Post Function to create a subtask based on some conditions within a transition between "submitted" and "in-progress"; but I'm having issues with ScriptRunner executing the conditions correctly.

Goal:

I want a subtask to be created if users have previously selected specific values for specific customfields prior to the transition.


For example:

"Client" customfield - Single Select (Client 1, Client 2, Client 3)

"Request Type" customfield - Single Select (RT 1, RT 2, RT 3, RT 4)

"Platform" customfield - Multi-select (Platform 1, Platform 2, Platform 3, Platform 4)

I only want the subtask to be created if:

Client = Client 2

Request Type = RT 1 or RT 3 or RT 4

Platform = at minimum Platform 3 is selected (Any other options can be selected / un-selected, but only create the subtask if Platform 3 is one of the values selected)

Previous tries:

I've tried numerous ways to get the above to work, and the code below is my latest attempt:

cfValues['Client'] == 'Client 2' & cfValues['Request Type'] == ['RT 1'||'RT 3'||'RT 4'] & 'Platform 3' in cfValues['Platform']*.value

Can someone please let me know what I'm doing wrong?

Thanks!

~Mike

2 answers

1 accepted

0 votes
Answer accepted
Michael
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 6, 2023

Hi all,

With the help of Ram, I was able to push my way through the issue. Please see below for the code that ended up working for my needs:

cfValues['Client']?.value == 'Client 1' && cfValues['Request Type']?.value in ['RT 1','RT 3','RT 4'] && cfValues['Platforms']*.value.contains("Platform 3")

 

0 votes
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 6, 2023

Hi @Michael

For your requirement, you modify your code to something like this:-

def sampleTextFieldValue = cfValues['Sample Text Field'].toString()

def number1Value = Double.valueOf(cfValues['Number 1'].toString()).toLong()

def number2Value = Double.valueOf(cfValues['Number 2'].toString()).toLong()

sampleTextFieldValue == 'Client 2' && number1Value in [1 as Long, 2 as Long, 3 as Long] && number2Value == 10 as Long

Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications.

Below is a screenshot of the Post-Function configuration:-

post_function.png

Instead of using:-

cfValues['Request Type'] == ['RT 1'||'RT 3'||'RT 4']

You should switch it to:-

cfValues['Request Type'] in ['RT 1', 'RT 3', 'RT 4']

Also instead of using:-

'Platform 3' in cfValues['Platform']*.value

You should switch it to:-

cfValues['Platform'].toString().contains('Platform 3')

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram

Michael
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 6, 2023

Hi again @Ram Kumar Aravindakshan _Adaptavist_

I'm a bit confused when it comes to your first section of code, would you be able to explain it a little?

def sampleTextFieldValue = cfValues['Sample Text Field'].toString()

def number1Value = Double.valueOf(cfValues['Number 1'].toString()).toLong()

def number2Value = Double.valueOf(cfValues['Number 2'].toString()).toLong()

sampleTextFieldValue == 'Client 2' && number1Value in [1 as Long, 2 as Long, 3 as Long] && number2Value == 10 as Long

I haven't ever dealt with "Double.valueOf" or ".toLong()" before and the sample code at the bottom of this section doesn't make much sense to me either (The 1 as Long, 2 as Long, etc.)

For the request type coding, should I have quotations ' ' around each option? I've never see it where there are beginning quotes around each option but not at the end.

Lastly, I'm assuming that instead of just adding a single "&" I have to add two in order for the condition to be an "AND" statement. Is there a specific reason why a single "&" doesn't work? Or is that just the way the language is?

As always I appreciate all the help!

~Mike

Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 6, 2023

Hi Mike,

In your last comment, you mentioned:-

I'm a bit confused when it comes to your first section of code, would you be able to explain it a little?

def sampleTextFieldValue = cfValues['Sample Text Field'].toString()

def number1Value = Double.valueOf(cfValues['Number 1'].toString()).toLong()

def number2Value = Double.valueOf(cfValues['Number 2'].toString()).toLong()

sampleTextFieldValue == 'Client 2' && number1Value in [1 as Long, 2 as Long, 3 as Long] && number2Value == 10 as Long

I haven't ever dealt with "Double.valueOf" or ".toLong()" before and the sample code at the bottom of this section doesn't make much sense to me either (The 1 as Long, 2 as Long, etc.)

So the reason this is done is because the default value that Jira has set to the Number field is of Double type.

Hence in Groovy, to remove the decimal point from the Number field, it needs to be converted to a String, then to Double, then to a Long.

Else if you just try:-

cfValues['Number 1'].toString().toLong()

it won't work as expected, as the value will Still preserve the decimal point, and the logic will fail.

Again, my example is different from your environment.

In your case, you don't seem to use any Numeric values, only Strings. I have explained that in the lower section of my previous comment.

In regards to why the value is once again set to Long, i.e.

number1Value in [1 as Long, 2 as Long, 3 as Long]

If this is not done, the boolean logic will fail. To be honest, I'm not sure why this is needed. But if you just try:-

 number1Value in [1, 2, 3]

it won't work as expected because the boolean logic will fail and return false, resulting in the Sub-task not being created.

To overcome this and ensure it returns true, the

number1Value in [1 as Long, 2 as Long, 3 as Long] 

the approach is taken.

I hope this helps to answer your question. :-)

Thank you and Kind regards,

Ram

Michael
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 6, 2023

Hi again @Ram Kumar Aravindakshan _Adaptavist_

Thank you for the explanation I greatly appreciate it.

Currently all the fields in question are either Select List (Single choice) customfields or Select List (multi choice) customfields. Would the above pertain to any numbers / options within these customfields?

I've tried to modify my coding per your above and for some reason I'm still not seeing the needed subtask being created.

cfValues['Client'] in ['Client 1'] && cfValues['Request Type'] in ['RT 1','RT 2','RT 3']

Since these are Select List customfields, would I need to an "?.value" to the end of each cfValues aspect? IE:

cfValues['Client']?.value in ['Client 1'] && cfValues['Request Type']?.value in ['RT 1','RT 2','RT 3']
Ram Kumar Aravindakshan _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 6, 2023

Hi @Michael

For multi-select, it will require some modification.

I'll update the example and post it here once it's ready.

Thank you and Kind regards,

Ram

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events