0

I am having a list of string as follows:

List<String> str=new List<String>();
str.add('blue1');
str.add('Green1');
str.add('Black1');

I want to search the above values into a custom setting named CustSetting__c. Could anyone please help me on this.

  • 1
    You'll likely receive a much better solution if you provide some context into why you're trying to use a custom setting for something like this. – Mark Pond Sep 11 '17 at 19:12
1

You would typically have a key-value pair stored in a custom setting. You get the value by passing the key. The key is the API Name you see in your custom setting.

Here is a minor example --> Note - code directly written into the post. Map<String,ColorList__c> colorlist= ColorList__c.getAll(); colorlist.get('Color List').green__c)

  • thanks for your response abhi!, but i am unable to apply this inside a forloop.. – Sab__Dev Sep 11 '17 at 19:10
  • Not sure if a custom setting would serve the purpose here. A custom setting is more like a configuration file, where you would know the names of the keys and attempt to retrieve the values from the keys. What is your requirement? – abhi Sep 11 '17 at 19:14
0

Here is an abridged tutorial to custom settings in APEX:

How to create a custom setting

Let's say your API name is custSetting__c like you mentioned. We'll give this custom setting 3 columns, the RGB values for the color.

// Retrieving custom settings
Map<String, custSetting__c> colors = custSetting__c.getAll();
system.debug(colors.get('Magenta').red__c + ', ' + colors.get('Magenta').green__c +', ' + colors.get('Magenta').blue__c);

// Creating new colors
custSetting__c newColor = new custSetting__c(
    Name = 'Teal',
    red__c = 0,
    green__c = 255,
    blue__c = 255
);

insert newColor;

// Looping through colors from a custom setting
for (custSetting__c color : custSetting__c.getAll().Values()) {
    system.debug(color.name);
}

Some considerations when using custom settings

Custom settings are stored in the cache and are readily available to all code, all the time. Because of this, there is a limit to how much data can be stored in custom settings. Be sure not to overload them.

Custom settings have historically been used for app configurations. SFDC has developed custom meta data types as a new way of storing this information so check those out as well. The big difference is that custom meta-data types are not stored in the cache and need to be queried using SOQL. Custom meta-data types DO NOT impact SOQL query limits.

If you do not need the data to persist permanently, consider using static lists or enums instead.

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.