Forums

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

JWME create subtasks script - How to optimize it

Chris Thomas
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.
August 1, 2022

Just a bit of background on me: I've been programming/scripting for 20 years, starting with C+ (forgot all of it) and currently consider myself advanced with PowerShell. No Groovy/Java experince.

Background on task: We have a security form that has 4 potential assignee's. I has approvers so I need to break it down into sub-tasks with each subtask having an approver. Instead of someone creating a subtask for each person I decided to automate it.

My automation got out of hand. So I have a script that figures out who needs a subtask based on the form that was submitted, but if I do something like this in the future, I would like to figure out how to simplify it.

Could someone take a look?

{% if issue.fields.customfield_10121 | find({"value":"Windows"}) != null %}
{% if issue.fields.customfield_10121 | find({"value":"Oracle"}) != null %}
{% if issue.fields.customfield_10121 | find({"value":"Phone"}) != null %}
{% if issue.fields.customfield_10121 | find({"value":"Jira"}) != null %}
NT Sub-task,Oracle Sub-Task,Phone Sub-task,Jira Sub-task
{% else %}
NT Sub-task,Oracle Sub-Task,Phone Sub-task
{% endif %}
{% elif issue.fields.customfield_10121 | find({"value":"Jira"}) != null %}
NT Sub-task,Oracle Sub-Task,Jira Sub-task
{% else %}
NT Sub-task,Oracle Sub-Task
{% endif %}
{% elif issue.fields.customfield_10121 | find({"value":"Phone"}) != null %}
{% if issue.fields.customfield_10121 | find({"value":"Jira"}) != null %}
NT Sub-task,Phone Sub-task,Jira Sub-task
{% else %}
NT Sub-task,Phone Sub-task
{% endif %}
{% elif issue.fields.customfield_10121 | find({"value":"Jira"}) != null %}
NT Sub-Task,Jira Sub-task
{% else %}
NT Sub-task
{% endif %}
{% elif issue.fields.customfield_10121 | find({"value":"Oracle"}) != null %}
{% if issue.fields.customfield_10121 | find({"value":"Phone"}) != null %}
{% if issue.fields.customfield_10121 | find({"value":"Jira"}) != null %}
Oracle Sub-task,Phone Sub-task,Jira Sub-task
{% else %}
Oracle,Phone Sub-task
{% endif %}
{% elif issue.fields.customfield_10121 | find({"value":"Jira"}) != null %}
Oracle,Jira Sub-task
{% else %}
Oracle
{% endif %}
{% elif issue.fields.customfield_10121 | find({"value":"Phone"}) != null %}
{% if issue.fields.customfield_10121 | find({"value":"Jira"}) != null %}
Phone Sub-task,Jira Sub-task
{% else %}
Phone Sub-task
{% endif %}
{% else %}
Jira Sub-task
{% endif %}

Just so you know, my brains are mush after keeping everything in mind, but it works!

Source I used to start this: 3. Configuring Post Functions (innovalog.com)

1 answer

1 accepted

0 votes
Answer accepted
David Fischer
Community Champion
August 1, 2022

Hi @Chris Thomas ,

did you look into using the "in" Nunjucks filter:

{% if issue.fields.customfield_10121 | field("value") | in("Windows","Oracle","Phone","Jira") %}

The "field" filter will return an array of the value of the "value" field for each selected option/checkbox, and the "in" filter will return true if at least one element of the array is in the list of values passed as parameters. 

Chris Thomas
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.
August 2, 2022

Thanks for the reply.

I thought about this off and on and I can't see how it can help in this situation. There is a custom form with 4 options and for every option a customer selects I need to return a specific response for the subtasks to be created. This code just tells me if a value is present, not direct to a proper response.

That said yesterday was the first time I did any Nunjucks and I can see some ways using "in" will be very helpful. For example in a project with lots of components. I really do appreciate the tip.

David Fischer
Community Champion
August 2, 2022

I'm not sure I understood your requirements properly. Your code is quite complicated so I'm not sure I understand what it's doing, but maybe I can guess: for each checked option of a checkboxes field (customfield_10121), you need to return a value which will become the summary of a new sub-task. Is that correct?

In that case, you can do this:

{% set subtasks = [] %}
{% if issue.fields.customfield_10121 | find({"value":"Windows"}) != null %}
{% set ignored = subtasks.push("NT Sub-task") %}
{% endif %}
{% if issue.fields.customfield_10121 | find({"value":"Oracle"}) != null %}
{% set ignored = subtasks.push("Oracle Sub-task") %}
{% endif %}
{% if issue.fields.customfield_10121 | find({"value":"Phone"}) != null %}
{% set ignored = subtasks.push("Phone Sub-task") %}
{% endif %}
{% if issue.fields.customfield_10121 | find({"value":"Jira"}) != null %}
{% set ignored = subtasks.push("Jira Sub-task") %}
{% endif %}
{{ subtasks | join(",") }}

Is that what you were looking for?

Chris Thomas
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.
August 2, 2022

That seems exactly what I was looking for.

Thank you very much for your time, now off to do a few hours of learning how it works!

David Fischer
Community Champion
August 2, 2022

Once you confirm it works, can you "Accept" the answer so that the answer can be found by other Community members?

Thanks,

David

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