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!

How It Works

  • • Write your solution in the code editor
  • • This challenge runs without connecting your own org
  • • Submit to check if your solution passes
  • • Use hints if you get stuck

Contest Alert

🏆 #CodeEveryDay September 2026 (Sep 1–15)Sponsored by FLXBL

Contest runs September 1 - 15. Complete challenges to climb the leaderboard!

1st & 2nd: Salesforce certification exam voucher see all prizes

Only the 15 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.

Wally can't hear you

Please sign in to access the AI Assistant

Sign In