Tuesday, December 04, 2018

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.

Thanks.

Friday, November 09, 2018

Experiment : Test.loadData . Upload test data with static resources

Test.loadData is a method that enables us to create test data by using Static Resource. This was released quite sometimes but I haven't use it intensively. I experienced using it to load data in bulk because I wanted to test governor limit.

Recently we plan to use this on our project instead of using test data factory.Pushing test data using csv files are much easier compare to code insertion and define relationship.

I did some simple proof of concept using code below :

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
@isTest
private class DataUtil {
    static testmethod void testLoadData() {
        // Load the test accounts from the static resource
        List<sObject> ls = Test.loadData(Account.sObjectType, 'AccountData');
        // Verify that all 3 test accounts were created
        System.assert(ls.size() == 3);

        // Get first test account
        Account a1 = (Account)ls[0];
        String acctName = a1.Name;
        System.debug(acctName);
        
        a1.Name ='New Name';
        
        update a1;
        
        Account a2 =[Select Id,Name from Account Where Name ='New Name'];
        
  //check if account get updated
        system.debug('After update a2 '+ a2.Name);
        
        Contact contact =new Contact (FirstName='TestContact',LastName='Lasting',AccountId=a2.Id);
        insert contact;
        
        Contact contact1=[select FirstName from Contact Where AccountId=:a2.Id];
        
        //yes , able to create contact on the account
        system.debug('After update contact1'+ contact1.FirstName);
        
        delete a2;
        
        List<Account> listAccount=[select Id from Account where Name='New Name'];
        //yes, able to delete
        system.assertEquals(0,listAccount.size());

    }
}

1. Can I update the record that I have loaded?
    Yes
2. Can I create child to the record that I have loaded?
     Yes
3. Can I delete the record that I have loaded?
    Yes.
4. Will insert trigger cover using Test.loadData?
    Yes, but validation rules is still applied.
5. Can I define relationship for example between Account, Opportunity and Contact?
     Yes you can.You can define it with mockup id in the column like below

When defining relationship, make sure that you run loadData method in proper order : master first then follow by child.In the example below, load Account first then Opportunity then Contact.

1
2
3
4
        // Load the test data  from the static resource in proper order
        List<sObject> lstAccount = Test.loadData(Account.sObjectType, 'AccountData');
  List<SObject> lstOpportunity = Test.loadData(Opportunity.sObjectType,'OpportunityData');
        List<SObject> lstContact = Test.loadData(Contact.sObjectType,'ContactData');

Some useful links to check out  :


Hope this help.