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
Nisha Babu 8Nisha Babu 8 

How to store query in custom setting

Hi,

I have to construct a query based on the custom fields added in the custom setting and have to store in a field inside the custom setting?
Is it possible for the query to take the values defined in the custom settig dynamically?

Regards,
  Nisha
 
Manuel Jiménez GonzálezManuel Jiménez González
All custom settings data is exposed in the application cache, which enables efficient access without the cost of repeated queries to the database. However, querying custom settings data using Standard Object Query Language (SOQL) doesn't make use of the application cache and is similar to querying a custom object. To benefit from caching, use other methods for accessing custom settings data such as the Apex Custom Settings methods.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_custom_settings.htm%23apex_System_ListCustomSetting_instance_methods
Rohit Sharma 66Rohit Sharma 66
Try the one below:
Add your custom setting name in place of "CustomSettingName__c"

Map<String, CustomSettingName__c> config;
config = CustomSettingName__c.getAll();
String queryFields = 'Select Id,CaseNumber,ContactId,OwnerId,Owner.Name,Status,Origin';
for(CustomSettingName__c m : config.Values()) {
    queryFields = queryFields + ',' + m.Name;
}
system.debug('*********'+queryFields);

queryFields = queryFields + ' ' + 'from Case';
system.debug('*********'+queryFields);

Please let me know if this solution works.