I have a batch class that inserts Account_Skill__c records. To ensure I don't have duplicate account skills, I created a unique field called Unique_Account_Skill__c and use Workflow to populate that field using the Account__c + ':' + Skill__c formula.

For some reason, some of my batches fail with this error:

First error: Insert failed. First exception on row 45; first error: DUPLICATE_VALUE, duplicate value found: Unique_Account_Skill__c duplicates value on record with...

Without any other reasonable way to tracking down where the duplicates are, I turn off the Workflow, and allow the "duplicates" to be inserted. I then export that data and analyze in Excel. As it turns out, there are no duplicates.

Why would the system raise errors in my batch class, stating duplicates exist, but when I export the raw data, there are no duplicates?

  • Duplicates (unique field) are checked long before the workflows even execute
    – cropredy
    Jan 2 '18 at 18:46
  • To clarify your question, please check if the error happens WITHIN a given batch class iterations or if it happens ACROSS batch iterations. To check this, set your batch size to, say, 1. If you get the error then, and not when inserting 100 records with a batch size of 200, then you have narrowed it somewhat. if you get it when inserting only 100 records with batch size 100, you will also know more. Come back and share your findings. Jan 2 '18 at 18:47

As it turns out, there are duplicates technically speaking. The issue is, when I created the unique field, I created it case-insensitive. I am using the Account.Id, and Skill__c.Id values concatenated, which I figured would provide me a unique composite key, but if you use Workflow to do this, it does not (if you don't consider case). See image below:

enter image description here

The RED highlight indicates duplication. Notice A2 and A3. The difference is the last "d" vs "D" in the Account ID. This was considered a duplicate (correctly), but not what I want. To solve the problem, I made the field case-sensitive:

enter image description here

As an aside, I originally setup the Unique_Account_Skill__c field to be populated with Process Builder, as I prefer it over Workflow. Process builder populated the field with the case-insensitive 18 digit IDs, whereas Workflow populates the fields with case-sensitive 15 digits IDs.

  • 2
    NB - You can use the CASESAFEID function in a Workflow Rule to get the same value.
    – Adrian Larson
    Jan 2 '18 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.