Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

Summary

The Scripted Task allows you to change message with a Velocity script. 

The objective of the scripted task is to modify the message 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 necessary.

How to use

  1. Once enabled, Add the custom script that performs the specific action:

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

  3. Add a Test Script. This is used to validate the outcome of the test case when the script has ran.

  4. Press Validate to test the script. Any issues will be displayed explaining why the script failed to execute.

  5. 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.

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 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 test case email looks after the main script is executed. A 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. 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.

Test Script examples

If you're adding a email address to your emails

$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

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

If you remove the Date header:

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

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

$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

#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

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

$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:

#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:

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:

$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.

  • No labels