8

I am new to custom settings.

My assumption is Custom Settings are part of application cache as such querying against custom settings do not count against SOQL limit.

This is my code snippet (executed from developer console).

Combinations__c is a custom setting.

String q='Creative';
String t='Dealer';
String a='Expand';
String sa='Included';

List<Combinations__c> sb = [SELECT ID FROM Combinations__c WHERE Queue__c=:q AND Type__c=:t AND Area__c=:a AND Sub_Area__c=:sa];
System.debug(sb.size());

This is my log as shown below.As you can see it is firing one SOQL query.

I am confused :(

enter image description here

I believed querying against Custom Settings do not count against limit.

Can someone explain why a SOQL is fired and is counted against the limit of 100 even when I am querying against custom settings ?

11

Querying custom settings with SOQL will surely count against limits.

The way custom settings should be used is via Apex methods for custom settings, not by using SOQL on them. Using SOQL defeats the purpose of using them.

To access data from list-type custom settings use the following:

List<Combinations__c> mcs = Combinations__c.getAll().values(); // Prefer this

This avoids SOQL on them. And then you can filter them as per your need using if statements.

To retrieve a custom setting by Name you can use getInstance or getValues methods:

Combinations__c myCS1 = Combinations__c.getValues('your custom setting name here');
  • Thanks. List<Combinations__c> mcs = Combinations__c.getall().values();//Prefer this and this avoids SOQL on them Using this I can intialize a list of the combinations (each list element comprising of one combination I assume). After this list (mcs) is intialized can we query against this list for a particular combination of queue,type,area and subarea ? – Varun May 24 '14 at 14:36
  • 2
    No .Never query.Why would you query use if and get the data required .Querying will cause governor limit – Mohith Shrivastava May 24 '14 at 14:38
  • 2
    @MohithShrivastava Querying allows you to filter and order the data because SOQL is good at that and will consume 1 of the 100 allowed queries only. So "never" is too strong a term. – Keith C May 24 '14 at 15:15
  • @KeithC Yes agreed on that.It depends on context .Like if logic is lot and somebody wants to save query i would go with If ,else i would go with query itself . – Mohith Shrivastava May 24 '14 at 15:18

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.