function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
herodotusherodotus 

Uniqueness Check Fails With Bulk Insert Test

To keep a record unique with respect to three fields, we setup:

 

  • hidden formula field to capture the three part unique key
  • visible text field with unique specified to hold the key
  • field update to move the formula field to the unique field
  • workflow rule to fire the field update on insert or whenever any of the three parts change.

 

 

This scheme works fine, however when running a test method with a bulk insert, the bulk insert fails.

The interesting thing is the insert call does not fail if records are inserted one at a time in a for loop.

The insert call does fail when the records are collected in a List in the for loop and inserted all at once.

 

I speculate that somehow the uniqueness key is cached and verified as unique more than once.

Perhaps the second time it gets verified agianst its cached-recently-verified value, as the error message indicates that the unique record id associated with the uniqueness violation is non other than the just inserted record. A typical error message looks like:

DUPLICATE_VALUE, duplicate value found: Unique_Commodity_LDC_LDC_Account__c duplicates value on record with id: a06Q00000009W5g 

 

The record id a06Q00000009W5g was generated as a result of the insert.

Any ideas on what might be causing the uniqueness-violation-on-bulk-insert behaviour?

 

Thanks 

MarkSilberMarkSilber
It could be related to how workflow runs asynchronously when updating your unique key. Is there a way to set the key in your trigger before insert / update?
herodotusherodotus

Mark,

 

Unfortunately no, using a trigger was not a viable option.

 

The original schemed used a trigger which looked for the unique conbination and threw an error when it was found.

The trigger worked well for small numbers, however it wasn't practical as we predict lots of bulk loads.

Looking up a uinique combination for each record in a trigger one-by-one quickly blows past the SOQL governor limits.