Versions Compared

Key

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

Summary

The Scripted Task allows you to change message with a Velocity script or ECMA Script script

The objective of the scripted task is to modify the message using a programmatic script, before running the standard JEMHCloud process using a programmatic script. Even though Velocity is a template language designed to render UI, Emails, etc. it can be used to modify objects programmatically including the message.

Please note this is an advance feature and minimal programming skill are is necessary.

How to use

...

  1. Once enabled, select the language in which you wish to write your script:

    Image Added
  2. Add the custom script that performs the specific action (the example shown here uses a Velocity script):

  3. Select or create a Test Case to use when testing the script. :

  4. Add a Test Script. This is used to validate the outcome of the test case when the script has ran .and must be written in the same language as the custom script:

  5. Press Validate to test the script. Any issues will be displayed explaining why the script failed to execute., underneath the script where the issues occurred:

  6. Make the relevant changes and then press Save.

Velocity Script

When creating a Script you may need to use some specific method that will perform specific actions. These methods can be found by pressing the velocity Context button. Here you will see some pages that contain methods that you may need to use. You can expand the velocity context to find links to javadoc for published Java classes.

...

  1. This script will then be used to modify incoming messages which are mapped to the Profile, before running the standard JEMHCloud process.

Adding a Test Case

A Test Case can be added by either creating your own email within the Test Case box or by selecting an existing Test Case from the Load Test Case list.

...

Using the Test Script

A way to verify that your email is being modified as expected is creating a Test Script that tests your main script. The test script will assert how a the test case email looks after the main your script is executed against it. A The test script is also written in velocity and it's composed of a list of assertions. An assertion is a condition that needs to be true in order to pass.

You can test the script before saving by pressing the validate button. This will allow you to solve any issues before saving the change. If you attempt to save a script which contains issues, these issues will be reported and you will not be able to save your script until you have resolved them.

It's highly recommended to test your script by adding many assertions to verify it works properly. If an assertion fails, the error below the script editor will show you the line and column number in the script that is failing. The presence of a Test Script is required.

...

Test Script examples

If you're adding a email address to your emails

Code Block
$taskUtils.getHeaderOrCreate($message, 'To').addValue('add.me.as.email.user@mycompany.com')

You can assert that the test case after being processed by using the following assert. You should see that the email will have two to: addresses, the original email address and the one the script adds

Code Block
$assert.assertEquals('Fernando <fernando@mail.com>,add.me.as.email.user@mycompany.com', $taskUtils.getHeader($message, 'To').getValueAsCSV())

If you remove the Date header:

Code Block
$taskUtils.getHeader($message, 'Date).remove()

The following assert will check whether a value is returned. Correct if value returned is Null.

Code Block
$assert.assertNull($taskUtils.getHeader($message, 'Date'))

Example Pre Proc Scripts

Replace From: Address

This example shows how to manipulate the From: address, to detect one domain and swap it out for another.

Script

Code Block
#set ($original = $message.getFrom()[0].getAddress().toLowerCase()) ##Extracts the Value found within the From header
#if ($original.endsWith("@thepluginpeople.com"))  ##checks if sender address is a specific address
    #set ($name = $original.split("@")[0])
    $message.setFrom($name + "@replaced.com") ##Changes original from address to be the extracted name and email address
#end

Example Email

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: CHANGE_SUBJECT!!!
From: andy@thepluginpeople.com
To: test@example.com
Content-Type: text/plain; charset=UTF-8

CHANGE BODY!!!

Test script

Code Block
$assert.assertTrue($message.getFrom()[0].getAddress().equalsIgnoreCase("andy@replaced.com"))

Script Configuration

...

Processing Report

Here you can see the example mail processing report generated by JEMHC for our support project, a user would have been associated if it didn’t exist, in this case, we’ve simply created a new portal user:

...

Extract address from name part and set as from address

If you are receiving emails that are sent from a system address with a personal part that contains the actual sender address. The script below will extract the actual sender address from the original personal part and will set this value as the sender of the email to be processed correctly.

Example Script:

Code Block
#set ($original = $taskUtils.getHeaderValue($message, "from").toString()) ##Extracts the Value found within the From header
#if ($original.contains("system@external.com"))  ##checks if sender address is a specific address
    #set ($address = $stringUtils.substringBetween($original, "(", ")")) ##Extracts the address from between the two brackets "("")"
    #set ($personal = $stringUtils.substringBetween($original, '"', "(")) ##Extracts the real senders personal part (Name)
    $message.setFrom('"' + $personal + '"' + "<"+ $address +">") ##Changes original from address to be the extracted name and email address
#end

Example 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: CHANGE_SUBJECT!!!
From: "ryan example (ryan@example.com)" <system@external.com>
To: test@test.com
Content-Type: text/plain; charset=UTF-8

CHANGE BODY!!!

Test Script:

Code Block
$assert.assertTrue($message.getFrom()[0].getAddress().equalsIgnoreCase("user@example.com"))

Script Configuration

...

Processing Report:

Here you can see the example mail processing report generated by JEMHC for this Test Case, a user would have been associated if it didn’t exist, in this case, we’ve simply created a new portal user with the correct email address and name.

...

...

For Velocity examples, see Velocity Scripted Pre-Processing Examples.

For ECMA Script examples, see ECMA Script Scripted Pre-Processing Examples.