Sorted records with index letter

This script shows you how to output index letters each time a specific column changes. E.g. if you have sorted database with last name, this script will output an index letter each time the first character of the last name changes:


Arimedes, Gustavo
Alberts, Hans


Bertram, Maximilian
Boll, Tanya

For this example you need an “index” placeholder and other placeholders to output the record data. Add both in separate lines but without additional empty lines between them (the script will take care of that).

Give the index placeholder a generation chain and in the generation chain settings choose “Delete line” as last action. Then if no index letter is created, the index placeholder and its whole line will be removed. Add a custom script element, fill in the script below and adjust it to your needs.

To output all of your data in one text frame choose “inline merge” in export > multiple records.

* MyDatamerge Javascript (Examples:
* This script outputs an index letter each time the first letter of a specified data record changes
* To ouput them all in one text frame choose export > multiple records > inline merge
* 1. In the export settings > extras > record sorting: Activate sorting and select a column to use
* for sorting
* 2. Write the column title you just selected in the "recordField" below

var recordField = "Last Name";

// Get the record values for the current and the previous record
var prevRecordValue = prevRecord()[recordField];
var recordValue = currentRecord()[recordField];

// Handle case: Row 1 - For row 1 the previous record is undefined
// So we return the first index letter
if (prevRecordValue == undefined && recordValue != undefined) {
    return recordValue.charAt(0);

// Handle case: Letter needs to change
if (prevRecordValue != undefined && recordValue != undefined) {

    var currentIndexLetter = recordValue.charAt(0);
    var prevIndexLetter = prevRecordValue.charAt(0);
    if (currentIndexLetter != prevIndexLetter){
        return currentIndexLetter;

// Else return empty string
// Empty strings allow you to remove a placeholder
return "";

Use this code in a Script element. Find more information here