This question runs in a pre-configured Salesforce environment - no personal connection required.
Write a method that returns the total count of Contact records in a specific department using SOQL aggregation.
Your method should:
- Accept a department name as a String parameter
- Use a SOQL query with COUNT() to get the total number of contacts in that department
- Return 0 if the department parameter is null or empty
- Return 0 if no contacts exist in the specified department
Examples
// Example 1: Count contacts in Engineering department
Integer count = countContactsByDepartment('Engineering');
// Returns: 5 (if there are 5 contacts with Department = 'Engineering')
// Example 2: Non-existent department
Integer count = countContactsByDepartment('Marketing');
// Returns: 0 (if no contacts have Department = 'Marketing')
// Example 3: Null department
Integer count = countContactsByDepartment(null);
// Returns: 0
// Example 4: Empty department
Integer count = countContactsByDepartment('');
// Returns: 0
Apex Code Editor
Welcome to Lightning Challenge!
Create an Account
Sign up to track your progress, earn points, and compete with others. Your solutions will be saved automatically.
Create accountHow It Works
- • Write your solution in the code editor
- • Connect your Salesforce org to test
- • Submit to check if your solution passes
- • Use hints if you get stuck
Note
You can test your code by connecting to Salesforce, but to save your progress and earn points, you'll need to create an account. Your solutions and achievements will be tracked automatically once you're logged in.