In this blog post, we’ll explore how to use Custom Metadata in Salesforce to determine if a plant is an Indoor or Outdoor plant. This is a great real-world use case to understand how to query metadata types from Apex, especially if you’re just starting to explore Salesforce’s advanced configuration capabilities.
We’ll write a method that takes the name of a plant as input and returns whether the plant is indoor, outdoor, or if the type is not specified.
Let’s dig in! 🌿
🧠 What Are We Trying to Do?
Imagine you’re building a gardening app or a nursery management system. You have a list of plants stored in Salesforce, and each plant has a type: Indoor or Outdoor.
But instead of storing this info in a custom object, you’ve used Custom Metadata — specifically, a metadata type called Plant_Information_Master__mdt.
We want to:
Accept a plant’s name as input.
Query the corresponding record from the metadata.
Return the Plant Type if it exists.
Return a default message like “Not Specified” if the plant isn’t found.
🔍 Code Explanation
We start with a class called ApexUseCaseTen. Inside it, we define a static method checkPlantType, which takes the plant name as input. We initialize the return variable plantType to 'Not Specified' as the default.
We run a SOQL query on the custom metadata. We look for a record where the MasterLabel (the metadata name) matches the input plantName.
✅ Tip:
MasterLabelis the display name of the metadata record, often used for lookups like this.
If exactly one matching record is found, we return its Plant_Type__c field — which stores whether the plant is Indoor or Outdoor.
If no matching record is found, we return the default value: 'Not Specified'.
✅ Why This Is Useful
This method is powerful because:
It lets you manage logic through metadata instead of hardcoding values.
You can add or update plant types without touching Apex code.
It shows how Apex and Custom Metadata can work together for dynamic logic.
This is a clean, scalable approach for any kind of category mapping not just plants!
🧾 Final Code Snippet
🎥 Watch It in Action!
Want to see this method in action inside Salesforce? Here’s a short video where I demo how to set up metadata records and use this Apex code to read from them.
Want to go further with this?
Learn it step by step in a course, or talk it through on a 1:1 call.
