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
Maxine SmithMaxine Smith 

Help with custom settings

Hi, I'm new to custom settings.  I have created one, and can put a query in my developer console window which gives my answer - select Name, NumberOfDays__c from DaysToDelete__c.  If I use the very same query in my Apex class, it returns no rows.  Why is this?  I must be doing something wrong.  The custom setting is a list and is public.  I know you don't need to use a query with custom settings so I also have a line in my code, DaysToDelete__c myCS1 = DaysToDelete__c.getValues('DayNumber');   This returns nothing too.  Please help!
Best Answer chosen by Maxine Smith
Maxine SmithMaxine Smith
I have found the issue!  I am running this in test, and in a test in Salesforce you don't seem to be able to see data unless you write in SeeAllData=true on the @IsTest line : @isTest(SeeAllData=true)

All Answers

Sampath KumarSampath Kumar
Hi Smith,

1. Custom settings can be accessed into apex using its key values.
Go to custom setting DaysToDelete__c, then click on manage, Click on the data what you want to refer. You will find the name. Make a note of the name and go to apex class and modify the line as below.

 DaysToDelete__c myCS1 = DaysToDelete__c.getInstance('name you have choosen'); 

Let me know if you have any issue in doing this.

Regards
Sampath

Mark this as best answer if this solves your issue.
Maxine SmithMaxine Smith
Hi Sampath, that returns null.
My code is DaysToDelete__c myCS1 = DaysToDelete__c.getInstance('DayNumber');
When I click Manage under the custom setting, in the Name column is 'DayNumber' so that is what I should be using?
 
Maxine SmithMaxine Smith
I have found the issue!  I am running this in test, and in a test in Salesforce you don't seem to be able to see data unless you write in SeeAllData=true on the @IsTest line : @isTest(SeeAllData=true)
This was selected as the best answer