4

I'm new to Salesforce, though an experienced Java developer and I'm trying to load my App's configuration using the recommended method of custom meta data.

I've found this quite poorly documented with most of the hits with Google being marketing material about how good it is, but rather lacking in good concrete examples of usage. These are the approaches I've tried, pulled from online examples, none worked for various reasons. I have two configurations defined, Thing_REST_stable & Thing_REST_latest.

    Thing_REST_stable config;
    System.debug('config : ' + config);  // Null as expected

    Thing_REST__mdt config = new Thing_REST__mdt();
    System.debug('config : ' + config); // Empty, unexpected

    Thing_REST__mdt config = new Thing_REST_stable();
    System.debug('config : ' + config); // Empty, unexpected

    Thing_REST__mdt[] config = [SELECT * FROM Thing_REST_stable];
    System.debug('config : ' + config); // fails to compile.

These configurations contain details such as the endpoint, version & credentials for external REST connections. The documentation for this is very poor: https://trailhead.salesforce.com/en/content/learn/modules/custom_metadata_types

  • How do I load these configs with Apex code?
  • How do I select Thing_REST_stable in live code?
  • How do I select Thing_REST_latest in my test code?
  • Can you point out some good examples, documentation?
  • What are the "Thing_REST_stable" and "Thing_REST_latest: in your code? What do you want to achieve through custom metadata? – Naval Sharma Dec 28 '18 at 11:26
  • I am trying to 'load my configuration', as per first sentence, they are the custom metadata objects, created via the UI. – Martin of Hessle Dec 28 '18 at 12:11
  • What is your use case mate? Looks like you want to query Custom Metadata? – Naval Sharma Dec 28 '18 at 12:13
  • I have an external REST interface and I do not want credentials in the code, I'm trying to load those, custom metadata seems to be the recommended way to do this, but loading configurations seems a pita in salesforce. This would be trivially easy in most languages. One line in java, System.getenv('endpoint') – Martin of Hessle Dec 28 '18 at 12:29
7

Custom Metadata isn't just a drop-in configuration load/save system. It acts in most cases like other metadata objects and variables in Apex and is subject to exactly the same syntax and limitations. Most of the issues you have below have to do with Apex and SOQL syntax, rather than Custom Metadata as such.

If the API name of your Custom Metadata record is Thing_REST__mdt, you must always refer to it as such. If you're maintaining more than one configuration, and a single record defines a configuration, you can populate the name of that configuration (such as Thing_REST_stable) in the record's DeveloperName field, and then query based on that field to access a specific configuration.

Examples

Thing_REST_stable config;
System.debug('config : ' + config);

This is an uninitialized variable, and like any other in Apex is null. Thing_REST_stable isn't the name of the metadata record, but might be the name of a specific configuration (as above).

Thing_REST__mdt config = new Thing_REST_stable();
System.debug('config : ' + config);

Custom metadata objects instantiated in code are blank, much like sObjects. Custom metadata classes do not have static methods like Custom Settings that instantiate them for you. You'll get a syntax error here because Thing_REST_stable and Thing_REST__mdt aren't the same types.

Thing_REST__mdt[] conf = [SELECT * FROM Thing_REST_stable];
System.debug('config : ' + config);

This is closest to being correct, but SELECT * is not valid SOQL, and you must use the full name of your object (Thing_REST__mdt) in your FROM clause. You might do

Thing_REST__mdt[] conf = [SELECT My_Field__c, My_Preference__c FROM Thing_REST__mdt WHERE DeveloperName = 'Thing_REST_stable'];

to source two specific fields from a defined configuration.

Thing_REST__mdt config = new Thing_REST__mdt();
System.debug('config : ' + config);

This is the same as the one above, but it's closer to being valid because you use your object's API name Thing_REST__mdt in both locations.

Storing Credentials

The best place to store credentials in Salesforce is not Custom Metadata, which can be read by anyone who has View Setup and Configuration permission. It's a Named Credential, which securely protects the credentials and handles OAuth authentication for you.

Custom Metadata and Custom Settings records can only be secured from users and system administrators in the context of a Managed Package, where they can be set to Protected.

  • Thanks a good answer, clear & too the point, sorry I don't have the score to up vote. – Martin of Hessle Dec 28 '18 at 13:30
  • The actual credentials have now been moved to apex 'Named Credentials' and retrieved using the same technique. – Martin of Hessle Jan 11 at 10:21
2

You can query custom metadata records with a simple SOQL and here is the syntax -

SELECT (ALL FIELDS WITH COMMA SEPARATED) FROM Thing_REST__mdt WHERE DeveloperName = '*metadata_record_developer_name*'

You can not use * like sql syntax. You have to provide all fields API names in the query.

Example :

String devName = 'Thing_REST_stable';

Thing_REST__mdt  thingsRestSetting = [ SELECT MasterLabel, DeveloperName FROM Thing_REST__mdt WHERE DeveloperName =:devName ];

You can pass the record developer name in the devName variable based on your environment. If the environment is testing then devName will be set to "Thing_REST_latest".

You can learn more about custom metadata from here.

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.