Friday, April 29, 2016

Playing with Salesforce Custom Settings (List based)

Querying Custom Settings

While working with Custom Settings in Apex, it is possible to use both Salesforce provided instance methods to access the Custom Settings or to use SOQL to query the data in the Custom Setting.

The Salesforce provided methods are well known and are documented here - Custom Settings Methods [developer.salesforce.com]

Using SOQL to query the Custom Setting is possible since the Custom Setting is similar to a Custom Object. Salesforce does not recommend this since

  • this will not use the application cache
  • will count towards your SOQL query limit
Try this
  • Create a custom setting with one field
  • In the Developer Console anonymous apex window, query your Custom Setting in a loop through SQL to see if you can hit the governor limit [NOTE: do not use SOQL in loops in your production code - I'm just trying this to make a point]


  • Now try retrieving the Custom Setting data using the instance methods - no SOQL is used.
List<anilc__Testing__c> testData2;
for (Integer i=0;i<101;i++) {
    testData2 = anilc__Testing__c.getAll().values();
}



Custom setting field type mapping

While researching this post, I was trying out a lot of the allowed fields in a Custom Setting and querying them in Apex. One of the things I was looking at was the field types - what do they map to in Apex? I didn't see this documented anywhere, so I'm listing them here, after trying them out



Custom Setting Field Type Apex field type
Currency Decimal. You can use Double as well
Date Date
DateTime DateTime
Email String
Number Decimal. You can use Double as well
Percent Decimal. You can use Double as well
Phone String
Text String
TextArea String
URL String
Checkbox Boolean





References

  1. Custom Settings Methods [developer.salesforce.com]





No comments:

Post a Comment