I've a test class for a controller which passes in the development Org but not in the destination/sandbox. Here's the error I get

System.DmlException: Insert failed. First exception on row 0; first error: DUPLICATE_VALUE, duplicate value found: unknown duplicates value on record with id: a2u************: []

Now, there is an External Id - unique field in the Object that I create in the test class which I believe is the problem maker here. I've used Datetime.now() to create a unique Id.

Datetime dt = Datetime.now();
String uniqueId = String.valueOf( dt.getTime() );

Sobject testObject = (sObject) Type.forName('My_Object__c').newInstance();
testObject.put('Custom_Name__c', 'TestLightning');
testObject.put('External_ID__c', 'TLHL' +uniqueId);
testObject.put('Reference__c', referenceId);
testObject.put('Asset__c', testAsset.Id);

insert testObject;

I've queried the existing data as well and I don't see any matching records or even the format I use for the unique field.

Disclaimer : I also have (SeeAllData=true) annotation since I've used Chatter Api to post feeds in the controller and I cannot remove the annotation. I understand this complicates things by getting to see the existing records in the environment.

UPDATE: I've removed the External_Id__c since it was not a required field but ended up getting even more vague error.

APP_Name: duplicate value found: unknown duplicates value on record with id: unknown

Also, I tried removing the history tracking for an object which I believe might be a problem as pointed here but I can't do that as well since I need feed tracking enabled on the object, the controller logic posts in chatter and this needs to be enabled during deployment(learnt it with a failed upload obviously).

I've also came across this but I don't get any line number as mentioned in the post.

Anything I should look into? Am I missing anything obvious?

Thanks in advance! Peace!

  • Is it possible to post your code?
    – Akram G
    Nov 13 '17 at 9:33
  • @AkramG Just updated the abstract code snippet. I've this in a TestDataHelper class which basically returns the object to test class and then DML is done in the method in test class. Nov 13 '17 at 9:57
  • @Vignesh Along with getTime concatenate that value with some random number using Match.random, let us know if you still face the issue. I think somewhere in the code the unique id is generated in same way. If it is running back to back might be it is giving you the same value. Nov 14 '17 at 7:16
  • @KiranMachhewar I've removed the External Id altogether since it was not a mandatory field. Nov 14 '17 at 7:18
  • @Vignesh Did you check this salesforce.stackexchange.com/questions/79799/… Nov 14 '17 at 7:23

Did your code inserting just one record or a list of records ? Your class calling another test method with the same logic? If that is the case you should use Test.startTest() at the start of test method and Test.stopTest() at the end of method so that the id's created will be valid only in that test method alone.

  • It just inserts a record. The DML is happening in the test method only within the Test.startTest() and stopTest() context. However, that issue is resolved now as I have removed the lookup to be populated. It is now giving me unknown error with no references to component or the line number. Nov 14 '17 at 8:44

My bad. We are unable to create even a simple Lightning App in the environment. We get the same vague error. Hence, we have narrowed down it as environment as the culprit. We'll refresh the sandbox and confirm it.

  • Just refreshed the sandbox and we were able to install the same package without any error. Nov 20 '17 at 10:08

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.