Loading Test Data ... made my life easier


I am still here, I forgot that I have a technical blog =)

Okay, I would like to share that something that maybe there for very long time, but I just utilized it recently.

My organization have something that we call Financial Calendar, it is different from our normal life calendar, where by if in our normal life a new year start on every 1 January, our financial start in different month.

Well, it is not big deal except in Salesforce we need to do test class, and most of our classes having logic such the object linked to calendar object, if new quarter come in populate the data etc.

Last time we just use @SeeAllData=true in order to access into this object from test class,but as you and I know it is not advisable as it may lead to other issue.

So we do something like this in our test class.We need to populate record for Financial Calendar in order to use it.

FinancialCalendar__c c1 = new FiscalCalendar__c();

The code goes down , to define the quarter.Oh, FYI, one record in FiscalCalendar store the quarter information such as year,quarter,begin quater date, end quarter date.Imagine if our logic to populate 6 quarter , not to include compare or search the quarter and year by user input plus if we have more than one class using the same object.

But at last, Salesforce introduce this cool stuff. Arrgghh!! Forget whatever code above.I directly query Financial Calendar record, export into .csv format and upload it into Static Resource.

Just to make this code reusable, I put it inside Test Data Factory class.

MyTestDataFactory class.Note FinancialCalendar is Static Resource Name.
public class MyTestDataFactory {
    //to create Fiscal Year
    public static void createFinCal (){
         // Load the data from the static resource
         List<financialcalendar__c> lstCal = Test.loadData(FinancialCalendar__c.sObjectType, 'FinancialCalendar');
public class MyTestClass {

  static testMethod void testReport(){
        List<financialcalendar__c> cals=MyTestDataFactory.createFinCal ();

Okay, done.While implementing this, I also faced some difficulty by assuming that the data is not inserted.So I try to put something like

insert lstCal;

right after loadData, and it give me an error because using load data is considered as insert.

*Clap*. Now we can have a mug of hot cocoa with some cookies =)


Popular Posts