JMWE (Nunjuck) postfunction not working as intended

Simon König
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.
October 2, 2024

Hello,

I tried to get a nunjuck post function (through jmwe) working on Issues with subtasks.

The postfunction in question is:

 

Subtask Workflow postfunction:

 

Set issue fields post-function;

Target Issue = Parent of current subtask;

Fields to update: "DoneDesc"

Code:

{{ parentIssue.fields.customfield_[ID of DoneDesc }} 

{% if issue.fields["Issue Type"].name== 'Subtask1' %}

{{ 'Subtask1:' }}

{{ issue.fields.customfield_[ID of DoneDesc Subtask1] }}

{% else %}

{{ 'Subtask2:' }}

{{ issue.fields.customfield_[ID of DoneDesc Subtask2] }} {% endif %}


 

What I want to accomplish: Fill the DoneDesc of my parent Issue when my subtask is done, but keep everything that was already in the Field. So if I e.g. have 2 Subtasks done, the Field should have all 2 SubtaskDoneDescs and whatever else I filled in beforehand.

If tested on my subtask (in the editor) this code does exactly that, but when tested with real issues and workflows the field is always overwritten, not appended.

Could someone explain to my if I did someting wrong here and why the editor Nunjuck template test works but not my Issues?

 

Kind regards

2 answers

Suggest an answer

Log in or Sign up to answer
0 votes
Manoj Gangwar
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 2, 2024

Hi @Simon König could you please try below nunjunk? 

{% set currentDoneDesc = parentIssue.fields.customfield_[ID of DoneDesc] | default('') %}


{% set newDoneDesc = '' %}

{% if issue.fields["Issue Type"].name == 'Subtask1' %}

    {% set newDoneDesc = 'Subtask1: ' + issue.fields.customfield_[ID of DoneDesc Subtask1] %}

{% else %}

    {% set newDoneDesc = 'Subtask2: ' + issue.fields.customfield_[ID of DoneDesc Subtask2] %}

{% endif %}


{{ currentDoneDesc }} 

{{ newDoneDesc }}

 

Simon König
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.
October 2, 2024

Hi and thanks for the reply. I tried your code and had the same problem:

Tested inside the editor I got:

>Old Desc

>Subtask1: New Desc

sadly, when used in the workflow the field was overwritten and only contained "Subtask1: New Desc".

 

One note: The ID of the used custom field is the same for all IDs, bc. the field is the same but used in different Screens for different Issues-Typed (Task/Subtask). Maybe that's a problem.

Manoj Gangwar
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
October 2, 2024

The issue you're facing where the DoneDesc field is being overwritten instead of appended could be related to how the post-function is handling the update when it executes in the workflow context. Since the custom field ID is the same across different issue types but is used on different screens, this could also affect how the data is being processed.

Let’s modify the approach to ensure that the existing value is correctly retrieved and appended to without being overwritten. 

// Get the existing value of DoneDesc from the parent issue

def parentIssue = issue.getParentObject()

def currentDoneDesc = parentIssue?.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_[ID of DoneDesc]")) ?: ''




// Initialize new description based on the subtask type

def newDoneDesc = ''




if (issue.fields["Issue Type"].name == 'Subtask1') {

    newDoneDesc = "Subtask1: " + (issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_[ID of DoneDesc Subtask1]")) ?: '')

} else if (issue.fields["Issue Type"].name == 'Subtask2') {

    newDoneDesc = "Subtask2: " + (issue.getCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_[ID of DoneDesc Subtask2]")) ?: '')

}




// Combine the existing value with the new description

def updatedDoneDesc = "${currentDoneDesc}\n${newDoneDesc}".trim()




// Set the new value back to the DoneDesc field in the parent issue

if (parentIssue) {

    parentIssue.setCustomFieldValue(customFieldManager.getCustomFieldObject("customfield_[ID of DoneDesc]"), updatedDoneDesc)

    // Ensure to persist changes to the issue

    issueManager.updateIssue(currentUser, parentIssue, EventDispatchOption.DO_NOT_DISPATCH, false)

}

Key Changes Made:

Retrieve Parent Issue: Ensure you are getting the parent issue using issue.getParentObject().

Defaulting Existing Value: If the DoneDesc field is empty, default to an empty string using the Elvis operator (?:).

Trim New Description: The updated description combines the existing value and the new description, ensuring no extra whitespace or newlines when combining.

Update Parent Issue: Ensure that the updated field value is set back to the DoneDesc field of the parent issue and peimport changes using issueManager.updateIssue.

Testing:

Test in Workflow: After making these adjustments, test the workflow again by transitioning a subtask to the status that triggers this post-function and check the DoneDesc field in the parent issue to ensure it appends correctly.

Simon König
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.
October 2, 2024

Hi and thanks for the input.

I think the Code you posted is meant for scriptrunner/groovy and not nunjuck, which jmwe cloud uses now.

 

I now setup my workflow and fields differently to get it to work.

The Subtasks now have a singled out Description which gets cast into the parent, which also has 2 different fields, each to hold information regarding the different subtasks.

This works for single Issues, but also not if repeated content is inserted.

Below my current setup:

Set issue fields post-function

Target Issue:Parent

Fields to Update:

1. DescofSub1

{% if issue.fields["Issue Type"].name== 'Sub1' %}
{{ parentIssue.fields.customfield_[parentfield1] }}
{{issue.fields.issuekey}} {{now}}
  {{ issue.fields.customfield_[sub1desc] }}
{%endif%}

2. DescofSub2

{% if issue.fields["Issue Type"].name== 'Sub2' %}
{{ parentIssue.fields.customfield_[parentfield2] }}
{{issue.fields.issuekey}} {{now}}
  {{ issue.fields.customfield_[sub2desc] }}
{% endif %}

Edit: This also sets Issuekey of the subtask and current date in the field.

 

0 votes
Simon König
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.
October 2, 2024

I also tried to move the parentIssue reference inside the if block, just to get rid of any scope-related problems. This did, sadly, not help

DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events