Using Test.loadData to import records with relationship

There are many resources and documents available around how to use Test.loadData to create test records in Apex class. As per best practice of writing Test classes in Apex, Its good idea to store master data (aka Seed, Reference data) in static resource and load it in Test classes using “Test.loadData” method. It will save lots of code around creating test records and at the same time easy to maintain. We can store records of Custom settings, standard or custom object which can be used frequently in our code. One of the best functionality to make writing Test classes more easier, As we don’t need to concentrate on writing code for creating data, time can be used to assert actual functionality.

Test.loadData and Static resource in Salesforce
Test.loadData and Static resource in Salesforce

So the question is, How can we load related records using Test.loaddata() method ? Simply by creating fake Salesforce Ids, that’s right !!! It is possible.

In this post, we will be loading two CSV files in static resource. One static resource file is used to create Account records and Other CSV will be used to create child contacts of Account.

Salesforce Developers interview questions – Most commonly used code snippets – part 21

201. Common Apex page attributes.

<apex:page sidebar="false" standardStylesheets="false" showHeader="false">

202. Declare Visualforce page as HTML5.

<apex:page docType="html-5.0" />

203. Visualforce page output as JSON.

<apex:page controller="ControllerName"  contentType="application/x-JavaScript; charset=utf-8" showHeader="false" standardStylesheets="false" sidebar="false">
{!jsonString}
</apex:page>