I remember when I was writing test class; Pricebook and the gang (Pricebookentry, Product) always giving me headache before I able to grab the concept. I also try to experiment on how to populate test data using Test.loadData on those object and quickly gave up as I was chasing for the dateline.
When I have a dateline, I always finding myself to choose familiar road although it can be slow because I know I can choose either to walk or run instead trying other new road which I unsure whether I able to walk or run or maybe eaten by monster.*who knows*
I was once asked question here , the answer give me a hope that Test.loadData seem possible to be try on those object. After doing some experiments, here my conclusion :
1. Define relationship with Id and the Id is not necessary all integer.If you notice in my previous post, you will notice that I use all number to represent Id BUT it does not mean that we need to use all number. For Id, we can use alphanumeric so for example below. Or we can define Account Id like this; Account1, Account2 whatever we think it convenient and readable as we might have lot of records to populate.
2. Use correct API Name regardless what.Yes, that is the mantra.*what on earth you were thinking if you are not using API NAME* However common mistake on relationship –use correct API Name that define the Id. For standard object for example Opportunity, it use standard name such AccountId store Account Id. In case we create custom object, please check what is API Name that define the relationship. Some of common mistakes is using AccountId while actually it is Account__c .
3. RecordType IS NOT INSERTABLE from Test.loadData, therefore USE REAL ID
Method does not exist or incorrect signature: void loadData(Schema.SObjectField, String) from the type System.Test
If our object requires Record Type, then use real RecordTypeId . Example above in Example 1 shows how I populate record type id in csv file. The disadvantage of this : Id might be different so you need to update your csv file but the advantage is it easy to update and I found that unlikely to have different recordtype id in sandbox.*I leave this for you to decide*
4. Standard PriceBook IS NOT INSERTABLE , therefore USE REAL ID
Please ensure the sequence of insertion.This rule applies regardless using csv or code. PriceBookEntry cannot be inserted without PriceBook and Product , Opportunity cannot be inserted without Account.Follow the rules, please put proper comment so other developer will not move the code. Put helpful Description in Static Resource and what it takes to help future generation.
I hope you find this article helpful. Let me know any method that you find useful.