Welcome to Zuora Product Documentation

Explore our rich library of product information

Advanced Accumulator code editor

The Advanced Accumulator code editor processes multiple events together, typically within time-based windows or grouped contexts. It is best suited for aggregation, summation, and grouped analytics.

Example

Suppose you collect daily transactions from different customers. With this code editor, you can calculate the total sales per customer each day or find the average order value per week. It works for batch or time-based processing where data is grouped and processed together.

exports.step = function(events, context) {
    if (events.length > 0) {
        // Use the first event as a base for the output
        const result = { ...events[0] };

        let totalSales = 0;
        let totalOrders = events.length;

        // Calculate total sales for this customer within the time window
        events.forEach(event => {
            const price = Number(event.price) || 0;
            const quantity = Number(event.quantity) || 0;
            totalSales += price * quantity;
        });

        // Add calculated fields to the result
        result.totalSales = totalSales;
        result.averageOrderValue = totalOrders > 0 ? totalSales / totalOrders : 0;
        result.customerId = result.customerId; // Group identifier
        result.eventCount = totalOrders;
        result.windowStart = context.windowStart;
        result.windowEnd = context.windowEnd;
        result.status = "aggregated";

        return result;
    }
    return null;
}

Capabilities

The code editor works with many events at once, usually within a time window (like every hour, day, or week). It is useful great for finding totals, averages, or counts. For example, summing up sales for each customer every day. It can also group data by fields (like customer ID or region) and analyze patterns within each group. The system collects events for a set period and then processes them together once the time is up. The code editor helps you summarize and analyze groups of data over time.

Since the code editor waits for a batch or time window to finish, results might not appear instantly. It also uses more memory since it stores multiple records until processing time. The code editor cannot look back at previous windows or handle ongoing state and only works with the events currently in its batch. Use it when you want time-based summaries or grouped calculations rather than real-time updates.

Configuration Metadata

Field

Mandatory?

Description

timeoutType

Conditional

Required only if triggerType = "Timeout". It defines whether time is measured by event timestamps (EventTime) or system time (ProcessingTime).

timeoutDuration

Conditional

Also required when triggerType = "Timeout". It sets the window size. For example, "1 hour", "1 day", etc. Without this, the system will not know how long to accumulate events.

eventTimeField

Conditional

Required only if timeoutType = "EventTime". It tells the system which field in your event holds the timestamp (e.g., "eventTime").

groupFields

Optional (but recommended)

Defines how to group events (e.g., by "customerId"). If you do not include it, all events are aggregated together into a single output, which may not be desired in most real-world use cases.

Example:

{
  "triggerType": "Timeout",           
  "timeoutType": "EventTime",         
  "timeoutDuration": "1 day",         
  "eventTimeField": "eventTime",      
  "groupFields": ["customerId"]       
}