Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Table of Contents
Note

In Closed Beta - not yet publicly available

Summary

Below are some example Velocity Scripts that can be used when setting a Dynamic Custom Field Default.

Script Context

API

Notes

message.getBody()

Get body content of email. Not available for preprocessing task.

message.getSubject()

Gets the Subject content of email.

message.getAllHeaders()

Get all headers of a message.

message.getHeader(name)

Get value of a message header

Parameters

  • name the name of the header to get

issue

If the email being processed has been associated with is commenting on an existing issue, this contains the issue data. Format then this context will contain the field values for the related issue. Allowing for this data to be used within the Script. Format used is as per the Get Issue response from the Jira REST API.

Note: This is only available within the Script Custom Field default and the Script Field Processor.

print(str)

Adds output that is recorded during execution and shown in the processing report.

Parameters

  • str (String) the string to be recorded

RegExp

Searches within string values for matching content.RegExp - JavaScript | MDN

setValue(value)

Sets the value for this Custom Field

Setting Custom Field Values via Script

Within the Script Custom Field Default values can be set within the script. Values are added to the Result Map in the Script Field Processor which will Overwrite any values already present in the resultMap.

To set the Custom Field Value you need to ensure that you use setValue(value) within the script to set the extracted value as the

Example

Code Block
setValue("FieldValue");

Using the Issue Context when commenting

Info

Currently cannot be used at edit time. A email/Test Case will need to be processed to test the script

When Commenting on issues you are able to use the issue to get values from the issue. for example getting the issue Key, Reporter or any Custom Field. Below are some examples on how to extract specific values.

Note: The issue context does not apply when creating an issue. This means that the issue context will return “null”.

Getting the Issue Key

Code Block
issue.key

Getting the issue ID

Code Block
issue.id

Getting a Custom Field Value

In order to get the value from a Custom Field you will need to define the Custom Field Id for the relevant field as the field name is not stored within the Issue context. This is to ensure that the value is gathered from the correct Field.

Code Block
issue.fields.customfield_10263

Gathering a System Field Value

For most of the system fields you will be able to define the field name and it will return the value that is set on that field. Below is an example of how to get the name of the Reporter user

Code Block
issue.fields.reporter.displayName

Example Scripts

Using Regex to extract a specific value from body and set as the field value

This example shows how Regexp can be used to extract a value from the email body and then set this value within the relevant Custom Field.

Code Block
var pattern = new RegExp("example:\\s([0-9]+)").exec(message.getBody());
if (pattern !== null)
{
    print("Matched Value: " + pattern[1]);
    setValue(pattern[1]);
}

Test Case

Code Block
MIME-Version: 1.0
Received: by 10.223.112.12 with HTTP; Sat, 18 Jun 2011 22:42:26 -0700 (PDT)
Date: Sun, 19 Jun 2011 17:42:26 +1200
Subject: This is the email subject
From: ryan@thepluginpeople.com
To: test@test.com
Content-Type: text/plain; charset=UTF-8

This is an example email
example: 100

Processing Report

Here you can see the example mail processing report generated by JEMHC for this Test Case. Within the report we see that the Custom Field Default successfully sets the value that was found in the email body.

...

Get a specific header value and then set as the Field value

This example shows how you can extract a value from a specific header and then set this as the value for the Custom Field.

Example Script

Code Block
var headerValue = message.getHeader('example');
if (headerValue)
{
    print("Value has been extracted from Header, Setting value: " + headerValue);
    setValue(headerValue);
}
else
{
    print("Header not found");
}

Test Case

Code Block
MIME-Version: 1.0
Received: by 10.223.112.12 with HTTP; Sat, 18 Jun 2011 22:42:26 -0700 (PDT)
Date: Sun, 19 Jun 2011 17:42:26 +1200
Subject: This is the email subject
Example: Test value
From: ryan@thepluginpeople.com
To: test@test.com
Content-Type: text/plain; charset=UTF-8

This is an example email

Processing Report

Here you can see the example mail processing Report generated by JEMHC for this Test Case. Within the report it states that a the Custom Field Default has applied this value.

...

Gather all email headers and set as the Custom Field value

This script will gather all of the email headers and values and will set as the Custom Field Value

Example Script

Code Block
var headerNames = Object.keys(message.getAllHeaders());
var headerValues = Object.values(message.getAllHeaders());
var value = "";

for (var i = 0; i < headerNames.length; i++) 
{
    print(headerNames[i] + ": " + headerValues[i]);
    var value = value + headerNames[i] + ": " + headerValues[i] + "\n";
}
setValue(value);

Test Case

Code Block
MIME-Version: 1.0
Received: by 10.223.112.12 with HTTP; Sat, 18 Jun 2011 22:42:26 -0700 (PDT)
Date: Sun, 19 Jun 2011 17:42:26 +1200
Example: Test value
Subject: This is the email subject
From: ryan@thepluginpeople.com
To: test@test.com
Content-Type: text/plain; charset=UTF-8

This is an example email
example: 100

Processing Report

As we can see the Report states that the Custom Field Default has set all of the email headers within the Paragraph Custom Field.

...

Filter by label (Content by label)
showLabelsfalse
max5
sortmodified
showSpacefalse
reversetrue
cqllabel in ( "custom-field-default" , "scripting" , "issue" ) and space = "JEMHC" and type = "page"