function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DianeMDianeM 

Spring '10 Issue with Custom Settings that affects testing

I believe that there is a bug associated with custom settings.

I used the migration tool to create custom settings in an org I use for integration. Test methods that reference the custom setting get a null value when I attemp to retrieve the custom setting.
After I went into the org and manually set a value in the custom setting (it is a public hierarchy) and then removed the value, subsequent executions of the test code executed as expected and the retrieval of the custom setting result in a non-null result.

I am concerned that I have to manually make a change to the custom setting before it is visible to the apex code. 
Best Answer chosen by Admin (Salesforce Developers) 
paul-lmipaul-lmi
When you migrate/deploy custom settings, they are not "initialized" by default.  Go into the Custom Settings section of Setup, hit manage, and create the org-wide defaults.  I ran into the same issue when using custom settings for our appexchange app.

All Answers

paul-lmipaul-lmi
When you migrate/deploy custom settings, they are not "initialized" by default.  Go into the Custom Settings section of Setup, hit manage, and create the org-wide defaults.  I ran into the same issue when using custom settings for our appexchange app.
This was selected as the best answer
DianeMDianeM

Yes - that is exactly what I am experiencing.  I am concerned that this is going to affect my ability to package this app for the app exchange.  My test code for the upload process has to skip everything that is associated with the custom settings.

 

Good to hear I am not the only one that saw this. 

DianeMDianeM
I did some more testing with this and when I package my application and upload it as managed beta, the custom settings are not initialized either.  I don't happen to depend on data in the custom settings but I now have to include in my code a test to see if I was able to access the custom setting.  This really is an oversight and is a problem if you are not aware of it.
mtbclimbermtbclimber

So it is a known limitation that custom settings defaults are not included in a pacakge.

 

This doesn't prevent you from testing them, as you seem to be aware.  There was an issue with creating them in a test context but that was resolved.

 

For those reading this later, you can test your setting whether defaults have been established or not by doing something like this:

 

 

@IsTest
private class customSettingsTests {

static testmethod void basicOrgDefaultTest() {

My_Setting__c s = My_Setting__c.getOrgDefaults();
if(s == null) {
s = new My_Setting__c(SetupOwnerId = UserInfo.getOrganizationId());
}
s.enabled__c = true;

Database.upsert(s);

Id accountId;
if(My_Setting__c.getOrgDefaults().enabled__c) {
Account a = new Account(name = 'test');
Database.insert(a);
accountId = a.id;
}

System.assert(accountId != null);
}
}

 

When this limitation is addressed, getOrgDefaults() should not return null effectively. No ETA on when this will be addressed at this time unfortunately. Custom Settings are a relatively new feature so I wasn't surprised to not find this as an Idea on the IdeaExchange. If you create one (which I encourage you to do), please tag it to the Visualforce & Apex  and Application Distribution categories and post a link on this thread back to it so others can find/vote on it easily. This will help us prioritize addressing this issue accordingly.

 

Thanks,

 

jwolfjwolf

The idea is here:

 

https://sites.secure.force.com/ideaexchange/ideaView?c=09a30000000D9xt&id=08730000000GrLgAAK

 

Please vote for it.