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
public class ApexUseCaseNine {
public static String checkGenerationType(Integer yearOfBirth){
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.
String genType = '';
We initialize a string variable to hold the generation label we’ll return.
if(yearOfBirth >= 1980 && yearOfBirth <= 1994){
genType = 'Millenial';
}
We first check if the year is between 1980 and 1994 (inclusive). If so, we assign the value “Millenial” to genType
.
else if(yearOfBirth >= 1995 && yearofBirth <= 2009){
genType = 'Gen Z';
}
Next, we check for the Gen Z range.
⚠️ Note: There’s a typo here — yearofBirth
should be yearOfBirth
. (We’ll fix this below.)
else if(yearOfBirth >= 2010 && yearOfBirth <= 2024){
genType = 'Gen Alpha';
}
Then we check if the year falls within the Gen Alpha range.
return genType;
}
}
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)
public class ApexUseCaseNine {
public static String checkGenerationType(Integer yearOfBirth){
String genType = '';
if(yearOfBirth >= 1980 && yearOfBirth <= 1994){
genType = 'Millenial';
}
else if(yearOfBirth >= 1995 && yearOfBirth <= 2009){
genType = 'Gen Z';
}
else if(yearOfBirth >= 2010 && yearOfBirth <= 2024){
genType = 'Gen Alpha';
}
return genType;
}
}
🎥 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.