Skip to main content
How We Reduced a Multi-Hour SFMC Automation to 30 Minutes Using a Dynamic SSJS Engine
Back to BlogAutomation

How We Reduced a Multi-Hour SFMC Automation to 30 Minutes Using a Dynamic SSJS Engine

November 22, 2025
10 min read
Dhanush MundrathiSalesforce Marketing Cloud Optimization Specialist
SFMCSSJSAutomationWSProxyData ExtensionPerformance

Nov 22, 2025 · 10 min read

How We Reduced a Multi-Hour SFMC Automation to 30 Minutes Using a Dynamic SSJS Engine

A deep-dive into how dynamic scripting, automated Data Extension generation, and query orchestration can transform large-scale SFMC systems.

By Eshwar Vemula

Salesforce Marketing Cloud Optimization Specialist

Overview

As Salesforce Marketing Cloud implementations expand, segmentation logic, data flows, and automations often grow faster than teams can manage them. Over time, many organizations find themselves operating massive, slow-moving automation frameworks filled with thousands of SQL Query Activities and Data Extensions.

Recently, our team redesigned a legacy SFMC workflow that:

  • 1000+ SQL Query Definitions
  • Executed hundreds of activities inside a single automation
  • Took over 4 hours to complete
  • Required updating every query whenever business logic changed
  • We replaced this architecture with a fully dynamic, SSJS-driven automation engine.

    The outcome?

  • 4+ hours → ~30 minutes runtime
  • 1000+ SQL queries → 0 static queries
  • Hundreds of steps → 2-step automation
  • Maintenance time reduced by 95%
  • This article explains the architecture, design approach, and business impact.

    The Challenge: The “One Query Per Segment” Architecture

    Many SFMC environments follow a pattern that eventually becomes unmanageable:

  • Thousands of Data Extensions
  • Each segment stored its output in a separate, dynamically-named DE.

  • One SQL Query per DE
  • Every DE required its own QueryDefinition.

  • Massive Automation Chains
  • Hundreds of query activities executed sequentially, increasing runtime and risk of failure.

  • High Maintenance Overhead
  • A small business logic update meant:

  • Editing every SQL query
  • Rebuilding activity steps
  • Running error-prone, multi-hour automations
  • This design works temporarily, but not at enterprise scale.

    The Solution: A Dynamic SSJS Processing Engine

    To eliminate manual maintenance and improve performance, we built a centralized Dynamic Per-Segment SQL Engine using SSJS and WSProxy.

    The engine performs all work dynamically:

  • Reads segment metadata from a central configuration DE
  • Builds the appropriate Data Extension name
  • Creates the DE automatically if missing
  • Injects segment parameters into a SQL template
  • Upserts and executes a QueryDefinition
  • Logs each operation
  • Marks each segment as processed
  • One script replaced the entire legacy structure.

    Key Capabilities of the New Engine

    #1. High-Performance Batch Processing

    Segments run in controlled batches (e.g., 400 per cycle), which:

  • Reduce timeouts
  • Improve stability
  • Increase throughput
  • #2. Dynamic SQL Generation

    A single SQL template is used for every segment.

    Variables such as:

  • segment filters
  • region codes
  • product attributes
  • personalization fields
  • are inserted dynamically.

    This removes the need to maintain thousands of SQL files.

    #3. Automatic Data Extension Creation

    If an output DE doesn’t exist, the script generates it with:

  • Required schema
  • Primary keys
  • Folder structure
  • Sendability configurations
  • This enables rapid onboarding of new segments.

    #4. Self-Healing QueryDefinitions

    The script intelligently handles QueryDefinitions:

  • Updates existing ones
  • Deletes & rebuilds broken ones
  • Creates new ones if missing
  • This ensures consistent, error-free execution.

    #5. Execution via WSProxy

    Using WSProxy to start QueryDefinitions ensures:

  • Faster execution
  • Fewer automation dependencies
  • Full API-level control
  • #6. Centralized Logging Framework

    Every segment execution logs:

  • Segment ID
  • DE name
  • Query key
  • Status
  • Runtime results
  • Error message (if any)
  • This provides audit-friendly observability and simplified troubleshooting.

    #7. Safe Restart Logic

    Segments are marked as processed only after the query succeeds.

    This ensures:

  • Automation resiliency
  • Safe restart points
  • No duplicate runs
  • Reliable batch processing
  • Before vs. After

    Legacy System

  • 4+ hour automation
  • 1000+ SQL QueryDefinitions
  • High-friction updates
  • Fragile workflows
  • Optimized System

  • Single SSJS engine
  • Dynamic SQL generation
  • Auto-created DEs
  • ~30 minute runtime
  • 2-step automation
  • Near-zero maintenance
  • Business Impact

    Metric

    Before

    After

    Runtime

    4+ hours

    ~30 minutes

    SQL Activities

    1000+

    0

    Automation Steps

    Hundreds

    2

    Logic Change Time

    Hours/days

    Minutes

    Scalability

    Low

    Extremely High

    Reliability

    Fragile

    Fully Logged & Stable

    The organization gained a future-proof framework that reduces operational costs and improves data readiness for marketing activations.

    Conclusion

    This project demonstrates how legacy SFMC architectures can be transformed using modern, dynamic scripting models. A single SSJS engine can replace thousands of static components, enabling:

  • Centralized control
  • Faster performance
  • Greater resiliency
  • Rapid iteration
  • Enterprise scalability
  • By modernizing the architecture, the organization now processes segments in minutes rather than hours—and updates logic in seconds rather than days.

    Ready to Transform Your Marketing?

    Our team of Salesforce experts can help you implement these strategies and achieve similar results.

    Schedule a Consultation

    About the Author

    Dhanush Mundrathi

    Salesforce Marketing Cloud Optimization Specialist