Our new Appfire Documentation Space is now live!

Take a look here! If you have any questions please email support@appfire.com

Fetch the value of a multi-value field when you know it has only one value

Abstract

This code snippet retains the only value of a multi-valued type field which can either be an array of objects or a set of values. This snippet is useful when you know that the array has only one value and hence by applying the first or last methods you can retain it. 

Logic

Use the first/last filter to get the only object of the field and access the specific field for the required value.

Snippet

//Access the only object of a multi-valued field using first/last filter followed by a method to access the specific value
if(issue.get("<Name of the field>"))
{
  issue.get("<Name of the field>").first().<Method to get the value>
}

Since you already know the collection of objects has only one value you could use the last() method as well.

Placeholders

Placeholder

Description

Example

<Name of the field>Name of the multi-valued fieldTechnical tasks
<Method to get the value>Method to get the valuegetValue()

Examples

The output of the code snippet is the only value of a multi-valued field which you could use in a Groovy expression, for example to:

  • Set a field in one of the Set field value post-functions or the Create issue post-function under the Set fields of new issue section. Examples:

    • Assign the issue to the Component Lead

      if(issue.get("components"))
      {
        issue.get("components").first().getComponentLead()
      }
    • Assign the issue to the author of the last comment

      if(issue.get("comment"))
      {
        issue.get("comment").last().getAuthor()
      }
    • Name of the issue link type of the only linked issue to the current issue

      if(issue.get("issuelinks"))
      {
        issue.get("issuelinks").first().getIssueLinkType().getName()
      }
  • Send an email to the first user of a Multi-user picker field in the Email issue post-function

    if(issue.get("Multi-user-picker field"))
    {
      issue.get("Multi-user-picker field").first()
    }

References