Skip to main content

Test.loadData in TestMethod

Use Case - 

Tired of writing many lines of codes just to create test data in test methods. Also, over the time, because of change in business requirement, you need to add/remove/modify fields from those test records. If yes, then Test.loadData() is best for you.
Say we want to write a test method, in which we need to insert account records and its associated contact records. Before Test.loadData(), the only option was to create explicit account records insert them and then create contact records. Also you need to establish relation between account & contact records. If you need 1000 account & 2000 contact records, you can assume how many line of codes you need to write just for creation of test records.

Solution - 

Use Test.loadData() to create records using csv file and using just a single line of code. Taking above problem in mind, let us discuss how to do it. Let us first create a new field (FakeId ) (API Name - FakeId__c) on both Account & a new field (Fake ParentId) (API Name - Fake_ParentId__c) Contact objects. To keep things simple, keep the data type of this field on both Account & Contact object same (say Integer with lenght 5). Next step, let's create two different csv files. In both files we will have value of fields of respective objects. First row in both csv files will have API Name of fields for which we are inserting records. Remember to have value for FakeId in Account & Fake ParentId for contact. Also, as per your requirement map value of FakeId in Account with Parent FakeId in Contact. Sample csv files may look like below.
For Account:-

For Contact: -




Now, we need to save these csv files in static resource with names say AccountCSV & ContactCSV. As soon as you will call belows:-

List<sObject> ls = Test.loadData(Account.sObjectType, 'AccountCSV');

Salesforce will insert test records from AccountCSV csv file and load that in list. For creating relation between account & contact records, you can use FakeId something like below:-


Comments

  1. Hi I tried using Test.loadData but the tests are failing in the packaging org as it has namespace, with the error message that the field does not exists.
    Any helpis appreciated. Thanks in advance !!

    ReplyDelete
  2. Why would you create a fake Id field, then iterate over what you just loaded, establish the relationship, then update the records when you can just populate the standard Id field and reference it during the data load?

    Add a column for Id on the Account CSV and populate it with TestAccount1, TestAccount2, and TestAccount3. Then add an AccountId field to the Contact CSV and populate it with TestAccount1, TestAccount2, and TestAccount3... It will achieve the same result without adding unnecessary fields, extra code, or requiring an update to records you just loaded.

    If a solution ever seems to be overly complicated or particularly clever, you're doing it wrong.

    ReplyDelete

Post a Comment

Popular posts from this blog

Mashup Integration in Salesforce

During preparation for TA certification exam, I came across a word Mashup for integration a number of times. I explored about it and below is description:- Mashups, sometimes called “composites,” are hybrid applications created by bringing together several data sources and Web services to create a new application or to add value to an existing application. Behind the scenes, mashups may require different levels of integration, depending on whether the mashed-up data is only meant to be viewed, whether it can be edited, and whether data is actually transferred between systems. There are three types of mashup:- Client Presentation Mashup - In this type of mashup the integration takes place strictly at the visual level. It makes possible to view data from two or more applications in a browser,  without actually moving data between the applications. Example - Google Maps. Client Service Mashup - As mashups evolve, they are becoming more complex and sophisticated. Client ser

Grant Access Using Hierarchies

Problem There is a custom object say 'XYZ' and OWD for this is set to ' Private ', which means record of this can be seen by only owner and users above in role-hierarchy and territory. However, to share this with other user, we can manually share it. The problem is that I don't want other users, who are above in role-hierarchy and territory of the user with whom record has shared, can see it. Solution We can un-check ' Grant Access Using Hierarchies ' check box for object 'XYZ' on 'Sharing Settings' page. We can go to Setup >> Security Controls >> Sharing Settings and click on ' Edit ' button. On the edit page, we can un-check ' Grant Access Using Hierarchies ' for required object.  Major uses of 'Grant Access Using Hierarchies' are:- If you disable the Grant Access Using Hierarchies option, sharing with a role or territory and subordinates only shares with the users directly asso

ReadOnly Annotation

Use Case:- You want to show up to 10000 record on single VF page. Count of records based upon some business requirement where number of records could go up to 1 million. So far, it was not possible to achieve above in VF page because of following limitations:- The maximum number of items in a collection that can be iterated over using components such as <apex:dataTable> , <apex:dataList> , and <apex:repeat> is 1000. Normally, queries for a single Visualforce page request may not retrieve more than 50,000 rows. Solution:- But with API version 23.0 , salesforce has introduced ' ReadOnly ' annotation which has following functionality/restriction:- The @ReadOnly annotation allows you to perform unrestricted queries against the Force.comdatabase. All other limits still apply. It's important to note that this annotation, while removing the limit of the number of returned rows for a request, blocks you from performing the following operations