Take a List of Contact records and upsert them using the Email field as the unique identifier. Each contact has FirstName, LastName, and Email populated. If a contact with the same email already exists, update their FirstName and LastName. Return the list of upserted contacts with their IDs. Return empty list if input is null or empty.

Examples:

Input: contacts = [Contact(FirstName='John', LastName='Doe', Email='john@example.com'), Contact(FirstName='Jane', LastName='Smith', Email='jane@example.com')]
Output: List of 2 Contact records with IDs populated
Explanation: Inserts 2 new Contact records using Email field as unique identifier and returns them with IDs

Input: contacts = [Contact(FirstName='John Updated', LastName='Doe Updated', Email='john@example.com')]
Output: List of 1 Contact record with existing ID
Explanation: Updates existing Contact with matching Email address and returns it with ID

Input: contacts = []
Output: Empty list
Explanation: Returns empty list for empty input

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 account

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

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.