+ Start a Discussion
Vinod TomarVinod Tomar 

Custom Setting values in Test Class


I am accessing custom setting values in my apex code and its working fine. When I am calling it through test class then No values gets in from Custom Setting. It always blank in test class. Can somebody help me to understand why custom settings values are not accessible in test class.



Vinod Kumar


Hey there,


Custom Settings are "Data" and as such are not part of the test-environment as of Spring '12.

Just like every object will have no records by default.


What you can do is either initially load the custom setting by using:

insert new CustomSetting__c(Field__c = 'Value');

 Or you could add the following annotation to your testmethod: (see http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_annotation_isTest.htm)

public static testMethod void myGreatTest() {


Beware that the latter solution will probably fail if you deploy this very testclass using a changeset that also creates the custom setting (as there really is no data yet).





Did you use 


@IsTest(SeeAllData) in you test class because without it you can't access custom settings record.

abhishek 1996abhishek 1996
just like object create a record.

public static testMethod void myTest() {
     CustomSetting__c cs = new CustomSetting__c();
        //cs.Other fiels values
     insert cs;

Mukesh Kumar 107Mukesh Kumar 107
1. Custom Settings are not visible in the Test Class by default.
2. Method-1 : You can use @istest(SeeAllData=true)
3. Method2-: You can create custom setting required for the test in the test method
4.Note that if you create custom setting through code within test method then you will not be able to use (SeeAllData=true)

Here is the code sample to create custom setting data in test class
    @testSetup public static void setup(){

        // insert all custom setttings which is being used in the Apex Code

        List<TriggerController__c> listTriggerController = New List<TriggerController__c>();

        TriggerController__c customSetTriggerController_Sales_Targets_Schedule_Trigger_1 = new TriggerController__c();
        customSetTriggerController_Sales_Targets_Schedule_Trigger_1.name = 'Sales_Targets_Schedule_Trigger_1';
        customSetTriggerController_Sales_Targets_Schedule_Trigger_1.Is_Active__c = true;
        customSetTriggerController_Sales_Targets_Schedule_Trigger_1.Object_Name__c = 'Sales_Targets_Schedule__c';

        TriggerController__c customSetTriggerController_ActivateSalesTargetTriggerLegacy = new TriggerController__c();
        customSetTriggerController_ActivateSalesTargetTriggerLegacy.name = 'ActivateSalesTargetTriggerLegacy';
        customSetTriggerController_ActivateSalesTargetTriggerLegacy.Is_Active__c = false;
        customSetTriggerController_ActivateSalesTargetTriggerLegacy.Object_Name__c = 'Sales_Target__c';

        TriggerController__c customSetTriggerController_Sales_Target_Trigger_1 = new TriggerController__c();
        customSetTriggerController_Sales_Target_Trigger_1.name = 'Sales_Target_Trigger_1';
        customSetTriggerController_Sales_Target_Trigger_1.Is_Active__c = false;
        customSetTriggerController_Sales_Target_Trigger_1.Object_Name__c = 'Sales_Target__c';

        TriggerController__c customSetTriggerController_Sales_Target_Trigger_2 = new TriggerController__c();
        customSetTriggerController_Sales_Target_Trigger_2.name = '    Sales_Target_Trigger_2';
        customSetTriggerController_Sales_Target_Trigger_2.Is_Active__c = false;
        customSetTriggerController_Sales_Target_Trigger_2.Object_Name__c = 'Sales_Target__c';

        insert listTriggerController;


Ashwini deshpandeAshwini deshpande
Refer "https://salesforce.stackexchange.com/questions/9565/how-to-get-custom-settings-in-apex-test-code" to create Custom setting in test class.
Aradhika Chawla 3Aradhika Chawla 3
@Ashwini deshpande Thanks for the answer. Its works perfectly.
Ad CadAd Cad
This is messed up - Custom Meta Data is available for Test code, so why not Custom Settings?