Convert Custom Settings
- Identify ways to convert custom settings for lists to custom metadata types.
- Refactor your org’s Apex code to support custom metadata types.
Make the Switch
One person, three days.
Yep, you heard it right. In less than a week, you can learn all about custom metadata types. You can convert up to 10 custom settings objects and refactor up to 200,000 lines of Apex code.
Follow these three steps to convert your custom settings.
Step 1: Convert Custom Setting Objects to Custom Metadata Types
First retrieve your app metadata, including the custom objects you’re using for configuration. Create corresponding custom metadata types for each object. You can do this step by hand or by transforming the objects’ XML using something like XSLT.
Regardless of the method you use to convert your objects, make sure that you use the same field names on your custom metadata types. This way you don’t have to change them in your Apex code later on.
When you finish creating your types, you can package all your metadata and deploy it to your org.
Step 2: Replace __c with __mdt
By now you’re comfortable with the idea that custom metadata types use the __mdt suffix instead of the classic __c suffix.
For each custom metadata type that you created in the previous step, change the object reference in all your Apex classes and triggers. You can use the Find and Replace tool in Eclipse.
Remember that the __mdt suffix is only for the type, not for its fields. You can leave the custom fields with the __c suffix.
Step 3: Replace Apex Code with SOQL Queries
After your metadata is deployed and your references are renamed, it’s time to get in the thick of your Apex refactoring. Replace Apex methods with SOQL queries and delete deprecated custom objects and their supporting code.
- Replace Apex methods, like getAll(), with SOQL queries, such as SELECT CustomField__c FROM MyMetadata__mdt.
- Delete deprecated custom objects and the code that supports those objects.
Ensure that you delete test code that creates mock records and post-install scripts that install records.
You’re ready to deploy your new code to production to finish up the conversion.
Congratulations! You learned what custom metadata types are, how to create them, and how to package them up and migrate them to other orgs. You’re ready to ditch your old app configuration methods and make more time in your life for things you actually want to do.
We’re only scratching the surface of the power of custom metadata types. With fearlessness and a sense of adventure, you can become an expert in no time.
- Trailhead Module: Data Modeling
- Salesforce Help: Create and Manage Custom Metadata Types Using CLI Commands