I use loadData method very often in my test methods for custom settings and objects where lookup fields are not necessary for my case. Now, I ran into a situation where I need to create hundreds of test records. They are all configuration records. The object has self relationship and it is 4 level deep.

Ex: An Account has 3 child accounts and each child has 4 grand-child accounts and each grand child has 10 children. In total they are in hundreds. The sad part is, I need all of them for my test cases.

I don't want to use seeAllData = true and wondering if I can build relationships with in CSV (Single or multiple) and use it in my test method. Or is there any other better way of doing this? Thanks.

  • 4
    Check this...salesforce.stackexchange.com/questions/54527/… – MIX DML Jan 6 '16 at 4:16
  • According to that thread, references with in static resource data set are not possible. I may end up creating 4,5 static resources. – Avinash Jan 6 '16 at 4:42
  • I think, creating multiple static resource for each level is only way to use Test.loadTest() – Jitendra Zaa Jan 6 '16 at 4:49
  • try using external ids – cropredy Jan 6 '16 at 14:25

I ended up creating 4 static resources. One for each level in relationship.

I gave Ids 0,1,2,3... for the top level.

Top Level

00,01,02... for the second level

Level Two

000,001,002... for the third level.

Level Three

No ids for the bottom level as it doesn't need to be referenced anywhere. enter image description here

It worked perfectly fine. I basically implemented Answer from this thread for multi level self relationships.

  • This is great Avinash N -- I wasn't aware the simulated IDs would work across multiple Test.loadData() calls. Nice work – cropredy Jan 7 '16 at 6:10

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.