17

I need to create Hierarchy custom settings in my test class to avoid the seeAllDate = TRUE

Little confused on how to create Hierarchy custom settings. Looked at the docs and other questions but don't see one on how to create in Apex:

Below is how I do for the List settings:

    custSettings__c settings = new custSettings__c();
    settings.Name = 'SF Environment Settings';
    settings.someField__c = 'Some Value';

    insert settings;
  • 2
    if you created one via the UI - simply use data loader to export all fields and see how they are constructed. – cropredy Aug 28 '15 at 21:32
27

You can use either of the two following constructs:

insert new custSettings__c(SetupOwnerId=UserInfo.getOrganizationId(), SomeField__c='Some Value');

Or:

custSettings__c settings = custSettings__c.getOrgDefaults();
settings.SomeField__c = 'Some Value';
upsert settings custSettings__c.Id;

Either way, basically you need to make sure SetupOwnerId is set to the organization's ID.

1

To do this with a hierarchy you have to change the 'SetupOwnerId' so the entry recognizes the User or Profile to apply the setting to. As a best practice I like to put the label in parenthesis in the name because when you create one through the Salesforce UI itself it will label the type that way by default.

For a specific user:

    User user1 = TestDataFactory.createUser('Mickey', 'Mouse', Constants.SYSTEM_ADMINISTRATOR, Constants.SYSTEM_ADMINISTRATOR, true);
    //^^ this creates the test user and inserts them into the database temporarily^^
    CustomSettingName__c setting = new CustomSettingName__c();
    setting.Name = 'CustomSettingName (User)';
    //^^ Name input + hierarchy type^^
    setting.CustomSettingFieldName__c = false;
    setting.SetupOwnerId = user1.id;
    //^^ this line tells the setting which user it applies to^^
    insert setting;

For a specific profile:

    User user1 = TestDataFactory.createUser('Mickey', 'Mouse', Constants.SYSTEM_ADMINISTRATOR, Constants.SYSTEM_ADMINISTRATOR, true);
    //^^ this creates the test user and inserts them into the database temporarily^^
    CustomSettingName__c setting = new CustomSettingName__c();
    setting.Name = 'CustomSettingName (Profile)';
    //^^ Name input + hierarchy type^^
    setting.CustomSettingFieldName__c = false;
    setting.SetupOwnerId = user1.ProfileId;
    //^^ this line tells the setting which profile it applies to^^
    insert setting;

Hope this helps!

0

For a test class, I like to simply upsert:

CustSettings__c settings = new custSettings__c.getInstance();
settings.someField__c = 'Some Value';
upsert settings;

The advantage being, is generally in a test class you don't care what the hierarchy is, you just care about what values your test class will be using.

If you want to test with a profile instead, you can always pass in the profile id to the getInstance() method. e.g.

CustSettings__c settings = new custSettings__c.getInstance(pid);
settings.someField__c = 'Some Value';
upsert settings;

The additional advantage of doing the upsert, is if say you set some of the custom settings in your data setup, and some in the test method, you'll get a nice automatic merge of all the fields you didn't override in your test method, and you don't have to worry about what if the setting already exists.

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.