2

I am trying to import some test data before tests with a csv file and the Test.load() method. I am not being able to figure out how to declare the load method for the RecordTypes I have in a file. Here is what I have:

static testMethod void init() {
    List<sObject> recordTypes = Test.loadData(?????, 'esb_record_type_test_data');
    List<sObject> accounts = Test.loadData(Account.sObjectType, 'esb_account_test_data');
    List<sObject> contacts = Test.loadData(Contact.sObjectType, 'esb_contact_test_data');
    List<sObject> customers = Test.loadData(Customer__c.sObjectType, 'esb_customer_test_data');
}

What should I declare in?

  • 3
    you do not need to load record types. They are available during apex testing. Using fake record types during test would not be a good practice as it would potentially alter the outcome to be different than in actual production day to day use with the real recordtpyes – Eric Feb 3 '15 at 19:32
  • Eric is correct. There should, however, be a RecordType.sObjectType that you can pass in. – Adrian Larson Jun 18 '15 at 22:19
  • I've opened idea to allow use of a RecordType DeveloperName in place of the RecordTypeId success.salesforce.com/ideaView?id=0873A000000PTveQAG – Alan Morey Jun 28 '16 at 18:25
7
  1. Record types are not separate object. Adding extra column in your CSV file with RecordTypeID should work. So while inserting from Test.Load(), record will be created based on record types.

  2. Alternately if there is change in recordtypeid use below syntax to get RecordtypeId and logic.

Example code:

Id RecordTypeIdValue = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Development').getRecordTypeId();

for(Account accountrecord : accounts) {
    accountrecord.Recordtypeid=RecordTypeIdValue;
}

Hope this helps.

Thanks Pradeep

  • So what you're saying is to load the records through the test.loaddata and then set the record type after? What record type do they have when they are loaded? I assume the default record type for the user running the test. – Brian Romanowski Jun 18 '15 at 22:10
  • The issue with putting the recordrypeid in the static resource is that it changes between environments. It is possible, you just need to have instance specific static resources to load your data. I would prefer to have one resource, load them, query the recordtypeid and then set it in the test. That way your test methods and resources are the same for every environment. – Brian Romanowski Jun 18 '15 at 22:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.