1

I'm creating some setup data for a test class. I'm wondering if there is annotation to turn off the Lookup filters during the test? I have removed the filters currently, but will need them after the tests pass.

This creates a custom Account where the fields have filters on normally:

//Lookup Filters will cause the insert to fail.
Relationship_Manager_Accounts__c RMAccount = new Relationship_Manager_Accounts__c();
RMAccount.RM_Rank__c =1;
RMAccount.Relationship_Manager_Contact__c = contact.get(2).id;
RMAccount.Account__c = account.get(2).id;

insert RMAccount;
return RMAccount;

1 Answer 1

3

No. If the filters are required, you must satisfy them. This usually means writing your test data to match those filters. Usually, I make a factory function for unit tests so that I can add such a filter requirement in just one place of code rather than hundreds.

For example, you might do something like this:

@isTest public class TestData {
    public static Account account { get { return new Account(Name='Test', Industry='Technology', ...); }
}

When you need a unit test with an account, you just do this:

@isTest static void test() {
    Account TestAccount = TestData.account;
    insert testAccount;
}

You can either use getter constructs, like demonstrated here, or more usually, a function that accepts various required configurable parameters, such as the matching record ID that satisfies the lookup condition. Or, you can even create a test suite that pre-populates a whole bunch of data all at once with all the required data/filters in place.

2
  • Thanks. I have a test data class similar to what you have stated that I can create Accounts, Contacts, etc. I should create an overloaded method when I have to change certain values to fit the filters. Just wanted to see if I was missing a feature.
    – kwiknick
    Commented Jul 14, 2015 at 19:29
  • @nwillard No, unit tests have to follow all the same rules. It'd be nice to be able to disable validation rules, for example, or other features can administrators can customize to make your unit tests sad.
    – sfdcfox
    Commented Jul 14, 2015 at 19:31

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .