Atlassian uses cookies to improve your browsing experience, perform analytics and research, and conduct advertising. Accept all cookies to indicate that you agree to our use of cookies on your device. Atlassian cookies and tracking notice, (opens new window)
The Plugin People
Teams
, (opens new window)

Enterprise Mail Handler for Jira Cloud (JEMHC)
  • Getting started
    Getting started
     This trigger is hidden
  • Documentation
    Documentation
     This trigger is hidden
  • Licensing
    Licensing
     This trigger is hidden
  • How-to articles
    How-to articles
     This trigger is hidden
  • Common Problems
    Common Problems
     This trigger is hidden
  • Support
    Support
     This trigger is hidden
  • Migrating from Server/DC to Cloud
    Migrating from Server/DC to Cloud
     This trigger is hidden
  • Frequently Asked Questions
    Frequently Asked Questions
     This trigger is hidden
  • General Data Protection Regulation (GDPR)
    General Data Protection Regulation (GDPR)
     This trigger is hidden
Results will update as you type.
  • Use Project Mappings
  • Use Priorities in Project Mapping Rules
  • Use Regexp Field Processor
  • Validate Mail Connections with Telnet
  • Opt-out of Google Analytics anonymous reporting
  • Integrate to Office 365 using Exchange Web Services
  • Understand How Project Association Works
  • How to generate acknowledgements for create and comment to the sender only
  • Forward an email to Jira as the sender
  • Set a default customer request type
  • How to export your profile
  • Changing the from: address used (statically or from a Custom Field)
  • Collect customer satisfaction feedback
  • Telegram Integration
  • Approving requests via email
  • Creating an outbound message connection
  • Forward notifications
  • Allow non-Jira (email-only) users to participate and be notified
  • How JEMHC Calculates Data Usage
  • Migrating from Data Center to Cloud
  • Support high outbound mail volumes
  • How Jira events turn into emails
  • Setting a custom field value
  • Send notifications via a SMTP Relay Service
  • Convert replies to sub task
  • How to use custom Directive Date Formatting
  • Determine non-business hours in Notification Templates
  • Prevent Mail Loops
  • Get notified via non-email connections when a message source/outbound goes offline
  • How-to grant JEMHC access (Project and Admin)
  • Creating Issues via Jira API
  • Test user lookup and Workaround User configuration
  • Differentiate Issues Created by JEMHC vs API
  • Using JSM Forms and Widgets
  • Using JEMHC in Jira with Third Party Systems
  • Customise the Content / Comment Header
  • Use At Prefix Field Processor
  • Enable client side filtering and sorting of messages
  • How to make comments internal
  • @Mention handling
  • Use Script Field Processor
  • Use Script Rule
  • How to disable inbound / outbound / event capture
  • View the JEMHC Issue / Comment Properties
  • Use JEMHC for JSM Notifications
  • Use X-Priority Headers
  • Configure Uni-directional communication between two Jira instances
  • Unsubscribe from notifications
  • Working with Google Groups
  • Manipulate webhook data in notification templates
  • Setting the Atlassian Team field
  • Upgrading JEMHCloud major versions
    Calendars
You‘re viewing this with anonymous access, so some content might be blocked.
/
Use Script Field Processor
Updated Aug 08

    Use Script Field Processor

    • 1 Summary
    • 2 Script Context
    • 3 Setting Custom Field Values via Script
    • 4 Using the Issue Context when commenting
      • 4.1 Getting the Issue Key
      • 4.2 Getting the issue ID
      • 4.3 Getting a Custom Field Value
      • 4.4 Gathering a System Field Value
      • 4.5 Guiding Comment visibility based on if sender is not the Reporter or Assignee
    • 5 Examples scripts
    • 6 Getting all recipients
      • 6.1 Using Regex to extract a specific value from body and set as a field value
      • 6.2 Get a specific header value and then set as a Custom Field value
      • 6.3 Drop an email if it is sent from a specific sender.
      • 6.4 Gather all email headers and set as a Custom Field value
    • 7 Dynamically setting a customer request type from X-Priority mail header
      • 7.1 Example Script
      • 7.2 Test Case
      • 7.3 Processing Report

    Summary

    Here we provide additional information and examples about what can be achieved within the Script Field Processor using JavaScript.

    Script Context

    This table shows a subset of what is available within the script field processor context.

    API

    Notes

    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.

    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) }

    message.getHeader(name)

    Get value of a message header

    Parameters

    • name the name of the header to get (Case insensitive)

    message.setHeader(key, value)

    Set the value of a message header

    Parameters

    • key (String) the name of the header to set

    • value (String) the value of the header to set

    issue

    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

    print(str)

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

    Parameters

    • str (String) the string to be recorded

    setOutcome(outcome, reason)

    Set the processing outcome of the script

    Parameters

    • outcome (String) the outcome type

    • reason (String) the reason for the outcome

    Note: outcome must be written in all capitals. Valid outcomes are (IGNORE, FORWARD, DROP)

    resultMap

    Field processor and Script Rule only. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map

    Note: Special characters (e.g. resultMap.put(“key”, “value with \“quotes\””) must escape the special characters.

    RegExp

    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

     

    {"serverDuration": 10, "requestCorrelationId": "99630af6cbf3410bb74cc586368a3143"}