JavaScript Pre-Processing Examples
Available Script Context
message.getBody()
is not available within the JavaScript Script Pre-Processing Task. Only header values can be extracted/set.
API | Notes |
---|---|
| Get all headers of a message. 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
|
| Adds output that is recorded during execution and shown in the processing report. Parameters
|
| Set the processing outcome of the script Parameters
|
| See below. https://nodejs.org/api/assert.html |
| Regular expressions can be evaluated within the script. This is often used for searches within string values for matching content. See RegExp - JavaScript | MDN for more information. |
Assertions
Textual Assertions
Use just ; in the assertion block to void the need for tests
See https://nodejs.org/api/assert.html for full Assert API documentation.
assert.equal('The actual value', 'An expected value', 'Helpful message');
Where the first value is an expression which captures a value in the email after it has been processed by your script, the second value is an expression indicating the value you expect to receive, and the third value is any message you wish to set. This final value will be output when an assertion fails, so it is recommended to set the message to something you will find useful.
Running the above Test Script example against any email will produce the following output (if you do not with to provide a message you can simply omit it)
A more realistic example is as follows.
Example Email:
Test Script:
Here we assert that the Subject has a certain value.