public without sharing class ContactCreator { @AuraEnabled public static void createContact(String fname, String lname, String email) { System.debug('Creating contact with email: ' + email); // Check for duplicate email List existingContacts = [SELECT Id FROM Contact WHERE Email = :email LIMIT 1]; System.debug('Found ' + existingContacts.size() + ' contacts with the same email'); if (!existingContacts.isEmpty()) { throw new AuraHandledException('A contact with this email already exists.'); } // If no duplicate found, create new contact Contact newContact = new Contact(); newContact.FirstName = fname; newContact.LastName = lname; newContact.Email = email; try { insert newContact; System.debug('Successfully created contact with email: ' + email); } catch (DmlException e) { System.debug('Error while creating contact: ' + e.getMessage()); throw new AuraHandledException('An error occurred: ' + e.getMessage()); } } }