{"id":5152,"date":"2016-01-06T05:58:44","date_gmt":"2016-01-06T05:58:44","guid":{"rendered":"http:\/\/www.jitendrazaa.com\/blog\/?p=5152"},"modified":"2016-01-06T06:03:21","modified_gmt":"2016-01-06T06:03:21","slug":"using-test-loaddata-to-import-records-with-relationship","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/using-test-loaddata-to-import-records-with-relationship\/","title":{"rendered":"Using Test.loadData to import records with relationship"},"content":{"rendered":"<p style=\"text-align: justify;\">There are many resources and documents available around how to use Test.loadData to create test records in Apex class. As per <a href=\"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/apex\/faq-writing-test-class-in-salesforce\/\">best practice of writing Test classes<\/a> in Apex, Its good idea to store master data (aka Seed, Reference data) in static resource and load\u00a0it in Test classes using &#8220;Test.loadData&#8221; method. It will save lots of code around creating test records and at the same time easy to maintain. We can store records\u00a0of Custom settings, standard or custom object which can\u00a0be used frequently in our code. One of the best functionality to make writing Test classes more easier, As we don&#8217;t need to concentrate on writing code for creating data, time can be used to assert actual functionality.<\/p>\n<figure id=\"attachment_5160\" aria-describedby=\"caption-attachment-5160\" style=\"width: 864px\" class=\"wp-caption alignleft\"><a href=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/01\/Test.loadData-and-Static-resource-in-Salesforce.png?ssl=1\" rel=\"attachment wp-att-5160\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5160\" src=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/01\/Test.loadData-and-Static-resource-in-Salesforce.png?resize=864%2C525&#038;ssl=1\" alt=\"Test.loadData and Static resource in Salesforce\" width=\"864\" height=\"525\" srcset=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/01\/Test.loadData-and-Static-resource-in-Salesforce.png?w=864&amp;ssl=1 864w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/01\/Test.loadData-and-Static-resource-in-Salesforce.png?resize=300%2C182&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/01\/Test.loadData-and-Static-resource-in-Salesforce.png?resize=768%2C467&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/01\/Test.loadData-and-Static-resource-in-Salesforce.png?resize=624%2C379&amp;ssl=1 624w\" sizes=\"auto, (max-width: 864px) 100vw, 864px\" \/><\/a><figcaption id=\"caption-attachment-5160\" class=\"wp-caption-text\">Test.loadData and Static resource in Salesforce<\/figcaption><\/figure>\n<p style=\"text-align: justify;\">So the question is, How can we load related records using Test.loaddata() method ? Simply by creating fake Salesforce Ids, that&#8217;s right !!! It is possible.<\/p>\n<p style=\"text-align: justify;\">In this post, we will be loading two CSV\u00a0files 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.<!--more--><\/p>\n<figure id=\"attachment_5155\" aria-describedby=\"caption-attachment-5155\" style=\"width: 188px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/01\/Account-CSV-file-for-Static-resource.png?ssl=1\" rel=\"attachment wp-att-5155\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5155\" src=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/01\/Account-CSV-file-for-Static-resource.png?resize=188%2C205&#038;ssl=1\" alt=\"Account CSV file for Static resource to use in Test.loadData method\" width=\"188\" height=\"205\" \/><\/a><figcaption id=\"caption-attachment-5155\" class=\"wp-caption-text\">Account CSV file for Static resource to use in Test.loadData method<\/figcaption><\/figure>\n<figure id=\"attachment_5156\" aria-describedby=\"caption-attachment-5156\" style=\"width: 320px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/01\/Contact-CSV-file-for-Static-resource.png?ssl=1\" rel=\"attachment wp-att-5156\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-5156\" src=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/01\/Contact-CSV-file-for-Static-resource.png?resize=320%2C238&#038;ssl=1\" alt=\"Contact CSV file for Static resource to use in Test.loadData method\" width=\"320\" height=\"238\" srcset=\"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/01\/Contact-CSV-file-for-Static-resource.png?w=320&amp;ssl=1 320w, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2016\/01\/Contact-CSV-file-for-Static-resource.png?resize=300%2C223&amp;ssl=1 300w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/a><figcaption id=\"caption-attachment-5156\" class=\"wp-caption-text\">Contact CSV file for Static resource to use in Test.loadData method<\/figcaption><\/figure>\n<p>As we can see in above image, dummy Salesforce Id is provided in Account CSV and same ID is used in Contact CSV file in column &#8220;AccountId&#8221;.<\/p>\n<p>Below code snippet proves that it is working.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\n\/**\r\n * Author\t:\tJitendra Zaa\r\n * Desc\t\t:\tSample Test class to demonstrate usage of Test.loadData to import related records \r\n * *\/\r\n\r\n@isTest\r\npublic class StaticResourceTest {\r\n\r\n      static testmethod void staticResourceLoad(){\r\n        \r\n        \/\/Load CSV file saved in static resource  \r\n        List&lt;SObject&gt; lstAcc = Test.loadData(Account.sObjectType,'AccountLoad_Test');\r\n        List&lt;SObject&gt; lstCon = Test.loadData(Contact.sObjectType,'ContactLoad_Test');\r\n        \r\n        \/\/Confirm that total number of accounts created are 5\r\n        System.assertEquals(lstAcc.size(), 5);\r\n        \r\n        for(Account a : &#x5B;SELECT Id, Name, (SELECT FirstName,LastName FROM Contacts) FROM Account where Id IN :lstAcc]){\r\n            \/\/confirm that every Account has associated child contact\r\n            System.assertNotEquals(null, a.contacts);\r\n            \r\n            \/\/confirm that every Account has exactly 2 contacts\r\n            System.assertEquals(a.contacts.size(), 2);\r\n        }\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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\u00a0it in Test classes using &#8220;Test.loadData&#8221; method. It will save lots [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[9],"tags":[325,197],"class_list":["post-5152","post","type-post","status-publish","format-standard","hentry","category-salesforce","tag-static-resource","tag-testclass"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":3694,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/apex\/faq-writing-test-class-in-salesforce\/","url_meta":{"origin":5152,"position":0},"title":"FAQ and Best Practices of Test Classes in Apex","author":"Jitendra","date":"February 15, 2014","format":false,"excerpt":"Here I am going to share few answers related to Test Classes, which is being asked many times to me by novice Programmers. Please feel free to post Comments if your question is not answered here. I will try my best to add those Questions in this article. If you\u2026","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5351,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/test-setup-method-in-apex-test-class\/","url_meta":{"origin":5152,"position":1},"title":"Test Setup method in Apex","author":"Jitendra","date":"February 22, 2016","format":false,"excerpt":"How to use setup method in Apex test classes","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1241,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/step-by-step-salesforce-tutorial-creating-trigger-and-test-cases-6-of-6\/","url_meta":{"origin":5152,"position":2},"title":"Step by Step Salesforce Tutorial \u2013 Creating Trigger and Test cases \u2013 6 of 6","author":"Jitendra","date":"October 11, 2010","format":false,"excerpt":"Step by Step Salesforce Tutorial \u2013 Creating Trigger and test cases \u2013 6 of 6 tutorials series","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Creating Trigger in Salesforce using force.com IDE","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/10\/Creating-Trigger-in-Salesforce-using-force.com-IDE.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":3050,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/my-favorite-top-10-features-of-winter-13-release-salesforce\/","url_meta":{"origin":5152,"position":3},"title":"My Favorite Top 10 Features of Winter 13 release &#8211; Salesforce","author":"Jitendra","date":"August 24, 2012","format":false,"excerpt":"Dear Friends, I am very excited to write this article about the cool Winter 13 features which i have added in My List. There are lots of lots of new features added in this release and i have made the list of my top 10 favorite features. Yesterday Salesforce published\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Salesforce Winter 13 Release Notes","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/08\/Salesforce-Winter-13-Release-Notes-300x212.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2681,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions-part-5\/","url_meta":{"origin":5152,"position":4},"title":"Salesforce Interview Questions &#8211; Part 5","author":"Jitendra","date":"January 24, 2012","format":false,"excerpt":"Basic concepts and Interview Questions of salesforce, Visualforce, Apex and SOQL","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2762,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions-part-6\/","url_meta":{"origin":5152,"position":5},"title":"Salesforce Interview Questions &#8211; Part 6","author":"Jitendra","date":"March 10, 2012","format":false,"excerpt":"Basic concepts and Interview Questions of salesforce, Visualforce, Apex and SOQL","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"Salesforce - External Id option while creating field","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/03\/Salesforce-External-Id-option-while-creating-field.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/03\/Salesforce-External-Id-option-while-creating-field.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/03\/Salesforce-External-Id-option-while-creating-field.png?resize=525%2C300&ssl=1 1.5x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/5152","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=5152"}],"version-history":[{"count":6,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/5152\/revisions"}],"predecessor-version":[{"id":5162,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/5152\/revisions\/5162"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=5152"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=5152"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=5152"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}