Code snippet to add the user selected in a single-user picker field to a Jira group.

Requirement:

Nunjucks code snippet to add the user selected in a single-user picker field to a Jira group.

Solution:

{%set user = issue.fields.customfield_10043 | field("_accountId")%} {% if user %} {{ "https://ABC/rest/api/3/group/user" | callRest(query={"groupId":"XYZ"}, body ={ "accountId": user }, verb=("POST"), options = { headers: { "authorization": "Basic XXXXX" } })}} {% endif %}

Replace:

  1. 10043 in line #1 with the ID of the "User Picker (single user)" custom field [the user selected in this field will be added to the Jira group]

  2. ABC in line #3 with the base URL of your Jira Cloud instance.

  3. XYZ in line #3 with the ID of the Jira group to which you want to add the user. You can easily get the group ID using Lookup group… feature of Nunjucks Template Editor.

  4. XXXXX in line #9 with your API token - check this.

 

  • The above script uses callRest JMWE Nunjucks filter and this Atlassian Jira Cloud REST API.

  • This Nunjucks code can be added in JMWE post-functions (that can be added in workflows and JMWE Actions: Event-based or Scheduled or Shared Actions)

  • If this script is added in a JMWE post-function that is added on the create transition of a workflow, select the checkbox “Delay the execution of this post-function” under “Delayed execution” and select a delay of 3 seconds.

 

References