OBLYTECH Logo
OBLYTECH Logo

Handling Partial Failures in Bulk Apex DML Results

Partial failure results from bulk Apex DML are a per-record list, not a single batch verdict. A run can finish with some rows saved and others rejected. Handling it well means reading a result for each record, matching each failure to its source row, and sending the failed rows to a named owner with enough detail to fix them.

Why a batch status hides the rows that matter

Many teams start a bulk job, check that it returned, and move on. That check answers the wrong question. A run can save a large share of its input and still reject a group of rows for reasons that have nothing to do with each other, such as a missing lookup value, a field that fails a validation rule, or a duplicate flagged by a matching rule. If the overall outcome is the sole signal anyone reads, the rejected rows drop out of sight until someone notices a gap in a report.

The fix starts in the code. Ask for a result for each input record, and keep track of which input row each result belongs to rather than relying on list position alone. That link is what lets a failure log point to one exact source row instead of a vague batch name. Without it, a team ends up searching the source file by hand for rows the run had already identified.

How to read the result for each record

A call that returns without an exception does not, by itself, show that each row was saved. Walk the results one at a time and decide the outcome for each entry. The sequence below keeps the mapping and the log in step.

  1. Before the call, build a map from each input position to its source row or external key.
  2. After the call, go through each result, match it to its entry in the map, and mark that entry as saved or rejected.
  3. For each rejected entry, copy the error message and any field name it reports into the failure record.
  4. Compare the count of saved and rejected entries with the size of the input. A mismatch means the map is wrong, so correct it before you report the run.

Attach a stable key to each record

Give each record a key before it goes into the call. An identifier from the source system works well because a person can search for it later and the failure log can cite it. If the source has no natural key, generate a row number at extraction and carry it through each transformation step. A number assigned after the call has no link to the source file, so it cannot help anyone trace a failure back. Stable keys also help when the same file runs twice, since a repeated row shows up under the same key in both logs.

Sort failures by cause before fixing anything

A pile of rejected rows looks like one problem. In practice it is usually several problems, and each one needs a different owner. Group the failures by the error message and the field involved, then look at the size of each group. A large group that shares a missing reference points to a defect in the load file. A single row with an unexpected character is a one-off that someone can correct by hand. Grouping first also stops you from fixing the same cause twice.

Causes that come up often in loads include:

  • A reference to a related record that does not exist in the target
  • A required value left blank in the source file
  • A value that fails a validation rule the business set on purpose
  • A duplicate that a matching or duplicate rule caught
  • A value in the wrong format, such as text in a date field

When the rejections come from validation rules, the rule may be right for people typing into a record and still block a bulk feed. For the integration side of that trade-off, read validation rules without breaking integrations. Fix the cause at the source when you can. Patching a few rows by hand and rerunning the full batch is a common way duplicates get into the target.

Log failed rows where an owner will see them

A failure log helps when a named person reads it. Decide the owner before the run starts. Then write the log to a place that person already checks, such as a shared table or a work queue. If the load runs through a loader tool instead of custom code, compare the options in choosing Salesforce data loader options for migration before you decide how results will be captured.

Fields each log entry should carry

  • The source file name and the row number or external key
  • The run date and a run identifier, so reruns can be told apart
  • The error message and the field it names
  • The object the write targeted
  • A status such as open, fixed, or rerun
  • The name of the person who owns the fix

Keep a count of saved rows in the same log. When someone asks whether a load was complete, the answer should come from the log, not from memory of the run.

Retry the rows that can succeed now

Rerunning the full input after a partial failure feels tidy, but it writes the rows that already saved a second time. That creates duplicates unless the retry checks for existing records first. Retry the rejected rows alone, and do so after the cause behind their group has been fixed. Build the retry input from the failure log using the same keys, so the second pass ties back to the first.

  1. Confirm that the fix for the cause group is in place, either in the source data or in the code that builds the input.
  2. Pull the open rows for that group from the failure log.
  3. Run the retry with the same keys and a new run identifier.
  4. Update each log row to fixed or still failing, based on the new result.

Reconcile counts after each run

When a run ends, close the loop with three numbers: the rows sent, the rows saved and the rows rejected. The saved rows and the rejected rows together should add up to the rows sent. The run record should also show how many rows from earlier runs are still open in the log, so the owner sees the whole backlog in one place.

A count that does not add up usually points to a mapping problem in your code rather than a data problem. Check the position map before you touch the source file. Then send the totals to the owner in the same message that lists the open rows, so the next action is clear to them.

Plan the handling before the first production load

Partial failure handling is easier to design in a test copy than to retrofit after a live load. Test with a sample that includes deliberate bad rows, such as a blank required value and a reference that points to nothing. Check that the log shows each one with the right key. Copies of production data go stale, so plan test runs around the copy you have and set a date for refreshing it. For that side of the work, see planning sandbox refreshes.

Batch size decides how much work each run does, so choose it with the platform’s processing limits in view. Read governor limits for admins before you set the size of each batch.

For help planning this work, see Salesforce implementation.

Questions about partial failures and failure logs

Should one failed row stop the rest of the batch?

Usually not, when the rows are independent of each other. Stopping partway leaves the target in a state that is harder to audit than a complete pass with a clear log. The exception is a run where later rows depend on earlier ones, such as a child record that needs its parent saved first. Decide that before the run starts, not during it.

How do I tell a temporary failure from a data problem?

Compare the failure with the rest of its group. When many unrelated rows fail with the same message at the same time, the cause is more likely the run itself, such as the user account running the load or a timing issue. When the failures follow specific values, the cause is the data. Retry the first kind after you check the run conditions, and correct the second kind in the source.

Who should own the failure log?

The person who owns the source data or the business process that produced the rows is usually the right owner. An administrator can back them up on platform-side issues. Name one primary owner for each load so that no one assumes a colleague has picked it up.

How long should failure logs be kept?

Keep them as long as your audit and reconciliation needs require, which often means until the load has been reconciled and signed off. Write the retention period into the migration plan so it does not depend on habit.

Should a retry reuse the first run identifier?

Give it a new one. A separate identifier lets you tell the first pass from the retry in the log and see how many rows each pass resolved. Reusing the identifier blurs those counts together.