TestDataFactory
TestDataFactory copied to clipboard
DUPLICATE_VALUE: duplicate value found
Hello, getting this error while running tests.
TestDataFactory.TestDataFactoryException: Unable to insert "QuoteLineItem.product2" records:
DUPLICATE_VALUE: duplicate value found: <unknown> duplicates value on record with id: <unknown> [Name=test0].
Seems 'TestDataFactory' doesn't count sames object number, and if creating record which depends on object 'X' and object 'Y' which depends also on 'X', two objects of 'X' will be created, but with same 'Name', that is causing my error.
It simply solved by specifying the name for one of 'X' objects, so another 'X' is default 'test0'. It would be great to make the counter, to prevent this error.
Hello @andrii-solokh
I you trying to link to the same object, you can do that in two steps like this example
Create 10 Cases related to 10 Accounts
Create a list of Acount sObjects and link them to a list of 10 Case sObjects
List<Account> accList = TestDataFactory.createSObjectList('Account', new Map<String,Object>{
'Description' => 'Account Description'
},10);
List<Case> caseList = TestDataFactory.createSObjectList('Case', new Map<String,Object>{
'Account' => AccList,
'Contact.Account' => AccList
},10);
@benahm Thank you for the fast response. But it seems you didn't get my idea.
AObj__c, req. fields :
- Lookup BObj__c
- Lookup CObj__c
BObj__c, req. fields:
- Lookup DObj__c
CObj__c, req. fields:
- Lookup DObj__c
In this situation, if you try to create AObj__c with only one action 'TestDataFactory.createSObject', it will produce error, because DObj__c will have same default name 'test0'.
Hello @andrii-solokh
I would do it like this
DObj__c dRecord = TestDataFactory.createSObject('DObj__c')
AObj__c aRecord = TestDataFactory.createSObject('AObj__c',new Map<String,Object>{
BObj__r.DObj__r => dRecord,
CObj__r.DObj__r => dRecord
});
Hope this help.
Ye, but if those should be different records, not same. And also if you don't care, just need to create test data as fast as possible, using one line.
TestDataFactory.createSObject.....
I think that could be useful.
I ran into this just trying to get all the tests in TestDataFactory_Test to pass, before I even started using the test factory. It's a definite issue in orgs that have Duplicate Rules on Name fields with Block actions.