Salesforce Development

Loops and Transform in Salesforce: Apex, Flow, and the New Era of Data Manipulation

Salesforce gives you three distinct ways to process and reshape data — and knowing which one to reach for, and when, is the difference between automation that scales and automation that creates maintenance problems six months later.

Ritik Jain
Sr. Salesforce Developer
June 11, 2026
8 min

Salesforce provides multiple ways to process collections of data  and choosing between them is one of the most consequential design decisions in any automation or integration project. Apex loops give developers precise control over complex logic. Flow loops bring iterative processing to admins without requiring code. The Transform element represents a newer, more declarative approach to data shaping that eliminates the need for loops in a growing range of scenarios. Understanding what each tool does, where it performs well, and where it falls short is the foundation for building automation that is scalable, maintainable, and does not quietly accumulate performance debt as data volumes grow.

1. Apex Loops — Precision and Control for Complex Logic

Apex is Salesforce's server-side programming language, and its loop constructs give developers full control over how collections are processed. Three loop types cover the range of Apex iteration scenarios.

Traditional For Loop

The traditional for loop is appropriate when the number of iterations is known at the time the loop is written — counting through a range, generating a fixed number of records, or executing logic a specific number of times.

for (Integer i = 0; i < 10; i++) {
    System.debug('Value of i: ' + i);
}

Enhanced For Loop

The enhanced for loop — often called a for-each loop — is the standard pattern for iterating over collections: lists returned from SOQL queries, sets, or the values within a map.

List<Account> accList = [SELECT Id, Name FROM Account];
for (Account acc : accList) {
    System.debug('Account Name: ' + acc.Name);
}

Looping Through Maps

When working with key-value structures, iterating over a map's key set provides access to both the key and its corresponding value in each iteration — a common pattern in bulkification logic where records are grouped by a parent ID.

Map<Id, Account> accMap = new Map<Id, Account>(accList);
for (Id key : accMap.keySet()) {
    System.debug('Key: ' + key + ', Name: ' + accMap.get(key).Name);
}

Apex Loop Best Practices

The most critical rule in Apex loop design is one that applies regardless of loop type: never place SOQL queries or DML statements inside a loop. Every SOQL query inside a loop multiplies governor limit consumption by the number of iterations. In a trigger context where the loop may process 200 records, a single SOQL inside the loop consumes 200 of the org's 100-query limit per transaction — a guaranteed failure path at scale. The correct pattern is to perform all SOQL before the loop, store results in maps or lists, and use those structures inside the loop without additional database queries. DML should be collected inside the loop into a list and executed once after the loop completes. Maps and sets improve lookup performance inside loops by providing O(1) access rather than iterating through a list to find a matching record. Use Limits.getDmlStatements() and Limits.getQueries() during development to verify that governor consumption is within safe boundaries before deploying to production.

2. Flow Loops — Iterative Automation Without Code

Salesforce Flow's Loop element brings collection iteration to admins and low-code developers through a visual interface. The underlying logic mirrors programmatic loops but is configured through Flow Builder rather than written in code.

How a Flow Loop Works

A Flow Loop operates across three components. The collection variable holds the list of records or values to iterate over — typically the output of a Get Records element, a manually defined collection, or a collection produced by an earlier Transform or Assignment element. The current item variable holds the record or value being processed in the current iteration — updated automatically by Flow at the start of each pass through the loop. Inside the loop, Assignment elements, Decision elements, and other logic components operate on the current item to build results, apply transformations, or evaluate conditions.

Common Use Cases

Flow loops are appropriate for updating each record in a collection before a single bulk Update Records operation, creating related records from input data, filtering a collection down to a subset that meets specific conditions, or summarizing values across a collection — running totals, counts, or comparisons.

Flow Loop Best Practices

The DML-inside-a-loop rule applies in Flow exactly as it does in Apex. Never place Update Records, Create Records, or Delete Records elements inside a loop. Collect the records that need to be created or updated inside the loop using Assignment elements that build a separate output collection, then perform the DML operation once after the loop completes. Keep loop logic as simple as possible. Loops compound complexity — a flow that is already moderately complex becomes significantly harder to read and maintain when it contains a loop with multiple decision branches inside it. If loop logic is growing complex, evaluate whether Apex or the Transform element is a better fit for the use case.

3. The Transform Element — Data Shaping Without Loops

The Transform element is a relatively recent addition to Flow Builder that represents a meaningful shift in how data manipulation is approached in declarative automation. Where a loop processes records one at a time through iterative logic, the Transform element operates on an entire collection at once — mapping input fields to output fields visually, applying formulas directly within the mapping interface, and producing a new data structure without requiring a single loop iteration.

What Transform Does

The Transform element maps input data to a new structure. It takes an existing collection as input, applies field mappings and optional formula transformations, and outputs a new collection in the shape you define. This is the core use case: converting one data structure into another — taking a collection of Opportunity records and producing a simplified structure containing only the fields an external system needs, for example, without writing Apex and without building a loop to process each record individually.

Where Transform Replaces Loop Logic

The Transform element is the right choice when the goal is to reshape data rather than perform conditional logic on individual records. If you need to convert a collection of records from one structure to another — different field selection, renamed fields, calculated values — Transform handles this in a single element. If you need to prepare a collection for an external API callout, Transform produces the exact structure the endpoint expects without the complexity of a loop-based construction. If your flow currently uses a loop, an assignment inside the loop, and a collection variable outside the loop just to map fields from one structure to another, Transform can replace that entire pattern with one element.

Where Loops Are Still Appropriate

Transform does not replace every loop scenario. Conditional logic — "update this field only if this other field meets a condition" — still belongs in a loop with a Decision element. Creating related records based on iteration logic still requires a loop. Any scenario where the logic applied to each record depends on the record's own field values is a loop use case. Transform is a data mapping and reshaping tool, not a general-purpose iteration replacement.

Choosing the Right Tool

The decision framework is straightforward when the use cases are understood clearly.

Use Apex loops when the logic is too complex for Flow, when performance at high data volumes requires the efficiency of server-side code, when the use case requires advanced data structures like maps and sets, or when the processing needs to integrate with custom Apex services.

Use Flow loops when the iteration logic is manageable in a visual interface, when admin ownership of the automation is the right long-term outcome, and when the use case is within what Flow's loop capabilities can handle cleanly without excessive element complexity.

Use the Transform element when the goal is to convert one data structure into another — field mapping, collection reshaping, data preparation for integration — and when no conditional per-record logic is required. Transform should be the first consideration whenever a loop exists primarily to move field values from one collection structure to another.

Official References

Apex Loop Types — Apex Developer Guide:developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops.htm

Traditional For Loops — Apex Developer Guide:developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/langCon_apex_loops_for_traditional.htm

Transform Element in Salesforce Flow:salesforceben.com — search "Transform Element Salesforce Flow"

Transform Elements Over Loops:automationchampion.com — search "Transform Elements Over Loops Salesforce"

"A loop is not always the answer to an iteration problem sometimes the answer is a better data structure, and sometimes it is a Transform element that makes the loop unnecessary entirely. The best Salesforce automation is not the one that works. It is the one that works, stays readable, and does not hit limits when volume doubles."

Salesforce Implementation That Actually Moves the Needle

Most Salesforce implementations go live. Ours go to work. We configure, integrate, and deploy Salesforce so your teams operate faster, your data works harder, and your business grows without the friction.