1

As my firm expands, I may have numerous Set that can each hold N different values. I want to move these Set values out of Apex and into Custom Settings or Custom Metadata, but there isn't a field for that, therefore I can't do it. I looked all over, but the only way to find the answer was to make a field for each String value in the set and creating multiple custom settings for each Set.

I don't want to create fields for each value in Set, can't it be dynamic like just create a record and BOOM. Is there any possible way to accomplish in this way?

Sample Code:

private static Set<String> addressForGoogle = new Set<String>{
        '[email protected]',
        '[email protected]',
        '[email protected]',
        '[email protected]'
        
};

private static Set<String> addressForYoutube= new Set<String>{
        '[email protected]',
        '[email protected]',
        '[email protected]'

};

private static Set<String> addressForGoogleMaps= new Set<String>{
        '[email protected]',
        '[email protected]',
};
1
  • 2
    I would recommend adding a custom metadata type with a custom field on it (you can name it type) then in your apex, you use that field to filter your records as needed and get all related addresses. that way you would add rows instead of columns to your object.
    – McCubo
    Commented May 10, 2023 at 7:43

1 Answer 1

2

There are a number of different options here. I've provided two, both based on Custom Metadata Types. You should avoid Custom Settings unless you need the data to vary by user or profile (and even then you can achieve similar with Custom Metadata Types these days).

Specific to purpose Custom Metadata

One approach, as suggested by @McCubo in a comment, would be to add a custom metadata type, such as AddressFor__mdt, with two fields:

  1. Type__c - a picklist of "Google", "YouTube", "GoogleMaps" and any other options you need.
  2. Email__c - an Email field for holding the email address you want.

You can then get all the instances that match the "type" you need. There are a couple of options for this:

  • Use SOQL - advantage: you get exactly the records you want; disadvantage: it counts towards your SOQL query limits.
  • Use static access to getAll() records then search through them for those that match the type you care about. Advantage: doesn't count as a SOQL query; disadvantage: slower handling since you have to write an Apex loop and perform your own matching of the type.

General purpose Custom Metadata

An alternative to this is to have a more general custom metadata type, e.g. NamedSetting__mdt, with just one custom field:

  1. Configuration__c - a long text area in which you store a JSON string.

In this case you care what the developer name is for the custom metadata type, these being values like AddressesForGoogle, AddressesForYouTube etc. Here you can get the required metadata record using getInstance(developerName) then parse the JSON.

Since you want a list of addresses, the JSON would look something like:

[
  "[email protected]",
  "[email protected]"
]

And you can parse this trivially using:

List<String> addresses = (List<String>) JSON.deserialize(record.Configuration__c, List<String>.class);

Note, however, that having used this approach means you can use different named records to store other arbitrary JSON-based configuration.

We have done this, but with one extra: we define JSON schemas that can be explicitly referenced in the CMT record in a second custom (String or URL) field called Schema__c. While the code doesn't use this, it's helpful to the admin because they can then know what format of JSON goes in the custom metadata type. Being actual JSON schemas they can even set up a JSON schema aware editor, such as VSCode, and have it help them edit the content with validation and auto-completion options.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .