Test.loadData on Pricebook, Product, PriceBookEntry and RecordType

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.

Example 1

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

There is no such thing like creating csv file with all RecordType record *at least for this time being* and call Test.loadData. It will throw compilation error :

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

*Do not even dream to insert Standard PriceBook * .Please remember in test class we need to use Test.getStandardPriceId() .HOWEVER we still able to populate PriceBookEntry using Test.loadData by using real Standard Price Book Id as example below. Note we still can populate custom PriceBook using Test.loadData .

5. First is first , last is last.Follow the sequence

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.



  1. I get System.UnexpectedException: Salesforce System Error: 1351849964-8162 (-18610796) (-18610796) for PriceBookEntry :( Any thoughts?

  2. Same error occured with me


Post a Comment

Popular Posts