Ever wondered if someone is a Millennial, Gen Z, or Gen Alpha just based on their year of birth? In this post, we’ll show you how to build a fun little Apex method that takes a person’s birth year and tells you which generation they belong to.
This is a simple and beginner-friendly use case that’s perfect for learning how to work with conditional statements, return types, and basic logic in Apex.
🧠 What Are We Trying to Do?
We want to create a method that:
Accepts a year of birth (as an
Integer)Checks where that year falls in known generation ranges
Returns the generation label as a string (e.g., “Gen Z”)
Here’s a quick reference:
Millennials: 1980–1994
Gen Z: 1995–2009
Gen Alpha: 2010–2024
This can be a great utility in CRM systems where you might want to segment users or customers by generation for personalization or targeting.
🔍 Code Explanation
We start by defining a class called ApexUseCaseNine with a static method named checkGenerationType. It takes a single input: yearOfBirth, which is an integer representing the person’s birth year.
We initialize a string variable to hold the generation label we’ll return.
We first check if the year is between 1980 and 1994 (inclusive). If so, we assign the value “Millenial” to genType.
Next, we check for the Gen Z range.
⚠️ Note: There’s a typo here — yearofBirth should be yearOfBirth. (We’ll fix this below.)
Then we check if the year falls within the Gen Alpha range.
Finally, we return the result string — which will contain the generation name or remain empty if the year doesn’t match any defined range.
✅ Why This Is Useful
This method is fun but also practical. You can use this logic to:
Personalize messages based on generation
Segment customer data for marketing
Add fun features to your Salesforce app like “Find Your Generation!”
It also teaches you how to use conditional if-else logic and return custom text values — which is a very common pattern in Apex programming.
🧾 Final Code Snippet (Fixed)
🎥 Watch It in Action!
Want to see this in action? Watch my quick demo showing how this method runs in the Developer Console and how you can tweak it.
Want to go further with this?
Learn it step by step in a course, or talk it through on a 1:1 call.
