Use Script Field Processor
Summary
Here we provide additional information and examples about what can be achieved within the Script Field Processor.
Script Context
This table shows a subset of what is available within the script field processor 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. This returns an object, allowing access to individual headers using the following: message.getAllHeaders()[headerName]; // Case sensitive
To loop through all headers: let allHeaders = message.getAllHeaders();
for (const header in allHeaders) {
print(header); //To retrieve header name
print(allHeaders[header]); //To retrieve header value (Case-sensitive)
} |
| Get value of a message header Parameters
|
| Set the 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. For more info about using Issue Data see: https://thepluginpeople.atlassian.net/wiki/spaces/JEMHC/pages/3861676043/Manipulate+Issue+data+in+Scripting+features#Showing-the-JSON-object-Values-and-structure |
| Adds output that is recorded during execution and shown in the processing report. Parameters
|
| Set the processing outcome of the script Parameters
Note: |
| Field processor and Script Rule only. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map |
| Searches within string values for matching content.https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp |
Setting Custom Field Values via Script
Within the Script Field Processor 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.
Example
resultMap.set("priority", "High");
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
For more info about using the Issue Data see: https://thepluginpeople.atlassian.net/wiki/spaces/JEMHC/pages/3861676043/Manipulate+Issue+data+in+Scripting+features#Showing-the-JSON-object-Values-and-structure
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.
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
issue.fields.reporter.displayName
Guiding Comment visibility based on if sender is not the Reporter or Assignee
Below is an example script that gathers the Reporter and Assignee
Examples scripts
Getting all recipients
var combined;
if (message.getHeader('to')!==null) {
combined = message.getHeader('to');
}
if (message.getHeader('cc')!==null) {
if (combined!==null) {
combined=combined.concat(message.getHeader('cc'));
} else {
combined=message.getHeader('cc');
}
}
if (message.getHeader('bcc')!==null) {
if (combined!==null) {
combined=combined.concat(message.getHeader('bcc'));
} else {
combined=message.getHeader('bcc');
}
}
combined=combined.toLowerCase();
print("Recipients: "+combined);
Using Regex to extract a specific value from body and set as a field value
If you want to retrieve specific text from the email body then you can do so by searching the email body with a Regular Expression that will extract the matching values. The below example will search the email body and if found it will then apply the value to the relevant Custom Field.
Example Script:
var pattern = new RegExp("example:\\s([0-9]+)").exec(message.getBody());
if (pattern !== null)
{
print("Matched Value: " + pattern[1]);
resultMap.set("example", pattern[1]);
}
Example Test Case:
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
example: 10
This is the email body
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 value was matched and that it has applied this value to the example Custom Field.
Get a specific header value and then set as a Custom Field value
This example shows how you can extract a value from a specific header and then set this value as a Custom Field value.
Example Script
var headerValue = message.getHeader('example');
if (headerValue)
{
print("Value has been extracted from Header");
resultMap.set('example',headerValue);
}
else
{
print("Header not found");
}
Test Case
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 value was extracted from the header and that it has applied this value to the example Custom Field.
Drop an email if it is sent from a specific sender.
This script will check if the sender matches a specific address and if so then it will drop the email so there is no further processing.
Example Script
var senderAddress = message.getHeader('from');
if (senderAddress == "ryan@thepluginpeople.com")
{
setOutcome('DROP', 'this sender should not create issues');
}
Test Case
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
Processing Report
Here you can see that the email has matched this condition and the email has resulted in being dropped.
Gather all email headers and set as a Custom Field value
This script will gather all of the email headers and set then as a Custom Field value.
Example Script
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 ";
}
resultMap.set("paragraph",value);
Test Case
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
Here you can see that the script has found all of the headers and their values and has set this onto the Paragraph Custom Field.
Dynamically setting a customer request type from X-Priority mail header
We can use
Example Script
var xprio = message.getHeader('X-Priority');
if (xprio!==null) {
print('X-Priority = '+xprio);
switch (xprio) {
case "1":
print('case 1');
resultMap.set("requesttype", "Technical support");
break;
default:
print('case default');
resultMap.set("requesttype", "Emailed request");
break;
}
} else {
print('no X-Priority found');
}
Test Case
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: test subject
From: andy@thepluginpeople.com
To: support@thepluginpeople.com
X-Priority: 1
Content-Type: text/plain; charset=UTF-8
CHANGE BODY!!!
Processing Report