Search Knowledge Base
Scripting (Javascript)
You can customize the merged results by using a custom script element.
Find some script samples here.
Basic rules for scripting:
- You have to return a string. Returning anything else will be interpreted as undefined.
- Strict mode is ON (https://www.w3schools.com/js/js_strict.asp)
- Asynchronous methods or setTimeout will not work or produce unpredictable results
Accessing the current data record from a script
Accessing the next data record from a script
Accessing the previous data record from a script
Accessing a specific data record from a script
Getting the current record number
Getting the highest available record number
Getting previous elements results as input
Throwing an error in MyDataMerge
Accessing the current data record from a script
/**
* currentRecord();
* @return {Object} Object with the current data record. Each data entry is available via key.
**/
// Example usage to get the "Last Name" field of the current record
var lastName;
// Always check for undefined
if (currentRecord() != undefined) {
lastName = currentRecord()["Last Name"];
}
return lastName;
Accessing the next data record from a script
/**
* nextRecord();
* @return {Object} Object with the next data record. Each data entry is available via key.
**/
// Example usage to get the "Last Name" field of the next record
var lastName;
// Always check for undefined
if (nextRecord != undefined) {
lastName = nextRecord()["Last Name"];
}
return lastName;
Accessing the previous data record from a script
/**
* prevRecord();
* @return {Object} Object with the previous data record. Each data entry is available via key.
**/
// Example usage to get the "Last Name" field of the next record
var lastName;
// Always check for undefined
if (prevRecord != undefined) {
lastName = prevRecord()["Last Name"];
}
return lastName;
Accessing a specific data record from a script
/**
* record(Integer);
* @param (Integer) the data record number - 1 is the first data record
* @return {Object} Object with a specific data record. Each data entry is available via key.
**/
// Example usage to get the "Last Name" field of record number 5
var lastName;
// Always check for undefined
if (record != undefined) {
lastName = record(5)["Last Name"];
}
return lastName;
Getting the current record number
/** * currentRecordNumber(); * @return (Integer) The current record number. **/ // Example usage to get the current record number var currentRecordNumber = currentRecordNumber(); return currentRecordNumber.toString();
Getting the highest available record number
/** * maxRecordNumber(); * @return (Integer) The max record number. **/ // Example usage to get the highest available record number var maxRecordNumber = maxRecordNumber(); return maxRecordNumber.toString();
Getting previous elements results as input
/** * input(); * @return (String) The input of all elements before the script element. **/ /** * Example scenario - You have 3 elements in your setup: * 1. datasource element with "First Name" column selected + a space character --> "Max " * 2. datasource element with "Last Name" column selected --> "Masters" * 3. script element * 4. static text element "Some other text" * With this command you will get the string "Max Masters" to work with, but not "Some other text" **/ var input = input(); // --> "Max Masters" return input;
Throwing an error in MyDataMerge
/**
* Throw an error in MyDataMerge by throwing an exception;
**/
throw("Some error message");