Write a method that takes a language name and makes an HTTP GET callout to the REST Countries API to find all countries where that language is spoken. Return a list of country common names, sorted alphabetically. Return an empty list if the language is not found, null, or empty, or if the API call fails.
API Endpoint: https://restcountries.com/v3.1/lang/{language}?fields=name
- Returns a JSON array of country objects
- Each object has a
namefield with acommonsub-field
Method Signature:
public static List<String> fetchCountriesByLanguage(String language)
Examples:
Input: language = 'swedish'
Output: ['Finland', 'Sweden']
Explanation: The API returns countries where Swedish is spoken, sorted alphabetically
Input: language = 'spanish'
Output: ['Argentina', 'Bolivia', 'Chile', 'Colombia', 'Costa Rica', 'Cuba', 'Dominican Republic', 'Ecuador', 'El Salvador', 'Equatorial Guinea', 'Guatemala', 'Honduras', 'Mexico', 'Nicaragua', 'Panama', 'Paraguay', 'Peru', 'Spain', 'Uruguay', 'Venezuela']
Explanation: Spanish is spoken in many countries, all sorted alphabetically
Input: language = 'invalidlanguage'
Output: []
Explanation: API returns 404 status for non-existent languages
Input: language = null
Output: []
Explanation: Null input returns empty list
Input: language = ''
Output: []
Explanation: Empty string returns empty list
Apex Code Editor
Welcome to Lightning Challenge!
How 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
Contest Alert
🏆 #CodeEveryDay August 2026
Contest runs August 1 - 31. Complete challenges to climb the leaderboard!
Only the 31 daily challenges shown during this contest count toward points. Earlier dailies don't carry over.