JavaScript examples
Summary
Below are some example Velocity Scripts that can be used when setting a Dynamic Custom Field Default.
Script Context
API | Notes |
---|---|
| Get body content of email. Not available for preprocessing task. |
| Gets the Subject content of email. |
| Get all headers of a message. |
| Get value of a message header Parameters
|
| If the email being processed is commenting on an existing issue, 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. |
| Adds output that is recorded during execution and shown in the processing report. Parameters
|
| Searches within string values for matching content.RegExp - JavaScript | MDN |
| 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
setValue("FieldValue");
Using the Issue Context when commenting
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
issue.key
Getting the issue ID
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.
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
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.
Test Case
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
Test Case
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
Test Case
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.