+ Start a Discussion
GordymanLG62GordymanLG62 

Accessing Custom Settings Object in Apex

I am at my wits end trying to get access to a custom settings object via Apex in a trigger and in a class.

 

Here's a simple class to get all the IDs from a custom settings object called ETO_LRT__c that has two fields Record_Type_Name__c and Record_Type_Id__c. I definitely have at least one dataset defined for this object.

 

When I try to access it via a utility class method, it always returns 0 values.

 

Here's the class

 

public class ETO_LeadUtils {

   public static Set<Id> ETO_AllRTs() {

   Set<Id> RTList = new Set<Id>();

   Map<String, ETO_LRT__c> CSMap= ETO_LRT__c.getAll();

   system.debug('Number of ETO LRTs = '+CSMap.size());

 

   for(String s : CSMap.keySet()){

    RTList.add(CSMap.get(s).Record_Type_Id__c);

   }

      return RTList;  }

 

 @isTest    public static void ETO_testAll(){

   ETO_LeadUtils.ETO_AllRTs();    }

}

 

What am I doing wrong?

 

 

GordymanLG62GordymanLG62

BTW I subsequently tried executing the following from the Developer Console (great new feature) and got a record returned, so I've verified there is actually a system-accessible record in the object.

 

List<ETO_LRT__c> lLRTs = [SELECT Record_type_Id__c FROM ETO_LRT__c];

 

I bet it's something simple.

GordymanLG62GordymanLG62

WHen I execute the SELECT Statement above in the Developer Console Execute window, it returns a single row.  But when I execute the same query from within the class method, it returns 0 rows.  My Custom Settings object ETO_LRT__c is is a Public List.  But it looks like it something to do with access.

aslamaslam
Hi michael
this is because SFDC enabled new feature for test classes which is @test(seeAllData=true).
By default it is false in test class. You need to make it on.

Thanks
aslam bari
http://forum.aslambari.com