// Mapping of email addresses to City, Country, and TimeZoneSidKey Map> emailToLocationMap = new Map>{ 'DomingoJr.Venci@aei.com' => {'City' => 'Cavite', 'Country' => 'Philippines', 'TimeZone' => 'Asia/Manila'}, 'Charisse.Fabian@aei.com' => {'City' => 'Cavite', 'Country' => 'Philippines', 'TimeZone' => 'Asia/Manila'}, 'Rexcee.Toledo@aei.com' => {'City' => 'Laguna', 'Country' => 'Philippines', 'TimeZone' => 'Asia/Manila'}, 'Chelseia.Aguilon@aei.com' => {'City' => 'Laguna', 'Country' => 'Philippines', 'TimeZone' => 'Asia/Manila'}, 'Allan.Resus@aei.com' => {'City' => 'Cavite', 'Country' => 'Philippines', 'TimeZone' => 'Asia/Manila'}, 'Jessica.Song@aei.com' => {'City' => 'Shenzhen', 'Country' => 'China', 'TimeZone' => 'Asia/Hong_Kong'}, 'Chris.Tian@aei.com' => {'City' => 'Shenzhen', 'Country' => 'China', 'TimeZone' => 'Asia/Hong_Kong'}, 'Hazel.Wu@aei.com' => {'City' => 'Zhongshan', 'Country' => 'China', 'TimeZone' => 'Asia/Hong_Kong'}, 'Roldan.Ronquillo@aei.com' => {'City' => 'Laguna', 'Country' => 'Philippines', 'TimeZone' => 'Asia/Manila'}, 'Daniel.Dearden@aei.com' => {'City' => 'Littlehampton', 'Country' => 'England', 'TimeZone' => 'Europe/Berlin'}, 'James.Brind@aei.com' => {'City' => 'Littlehampton', 'Country' => 'England', 'TimeZone' => 'Europe/Berlin'}, 'Kelly.Chiah@aei.com' => {'City' => 'Penang', 'Country' => 'Malaysia', 'TimeZone' => 'Asia/Kuala_Lumpur'}, 'Sally.Chng@aei.com' => {'City' => 'Penang', 'Country' => 'Malaysia', 'TimeZone' => 'Asia/Kuala_Lumpur'}, 'Shiny.Deng@aei.com' => {'City' => 'Shenzhen', 'Country' => 'China', 'TimeZone' => 'Asia/Hong_Kong'}, 'Patrick.Yu@aei.com' => {'City' => 'Shenzhen', 'Country' => 'China', 'TimeZone' => 'Asia/Hong_Kong'}, 'Tom.Yang@aei.com' => {'City' => 'Shenzhen', 'Country' => 'China', 'TimeZone' => 'Asia/Hong_Kong'}, 'Vivi.Li@aei.com' => {'City' => 'Shenzhen', 'Country' => 'China', 'TimeZone' => 'Asia/Hong_Kong'}, 'Donald.Hoffman@aei.com' => {'City' => 'Fort Collins', 'Country' => 'USA', 'TimeZone' => 'America/New_York'}, 'Marcela.Hirales@aei.com' => {'City' => 'Mexicali', 'Country' => 'Mexico', 'TimeZone' => 'America/Mexico_City'}, 'Francisco.Zarate@aei.com' => {'City' => 'Mexicali', 'Country' => 'Mexico', 'TimeZone' => 'America/Mexico_City'}, 'Jason.Nie@aei.com' => {'City' => 'Zhongshan', 'Country' => 'China', 'TimeZone' => 'Asia/Hong_Kong'}, 'Allen.He@aei.com' => {'City' => 'Zhongshan', 'Country' => 'China', 'TimeZone' => 'Asia/Hong_Kong'}, 'Angel.Wang@aei.com' => {'City' => 'Zhongshan', 'Country' => 'China', 'TimeZone' => 'Asia/Hong_Kong'}, 'Cliff.Zhang@aei.com' => {'City' => 'Shenzhen', 'Country' => 'China', 'TimeZone' => 'Asia/Hong_Kong'}, 'Declan.Porter@aei.com' => {'City' => 'Hongkong', 'Country' => 'China', 'TimeZone' => 'Asia/Hong_Kong'} }; // Fetch contacts created **today** List recentContacts = [SELECT Id, FirstName, LastName, Email, AccountId FROM Contact WHERE CreatedDate = TODAY AND AccountId = '001VV00000MYNn8YAH']; // Get Contact IDs Set contactIds = new Set(); for (Contact con : recentContacts) { contactIds.add(con.Id); } // Fetch existing Partner Users in one query Set existingPartnerUserContacts = new Set(); if (!contactIds.isEmpty()) { for (User existingUser : [SELECT ContactId FROM User WHERE ContactId IN :contactIds]) { existingPartnerUserContacts.add(existingUser.ContactId); } } // Fetch all existing usernames in one query Set existingUsernames = new Set(); for (User u : [SELECT Username FROM User]) { existingUsernames.add(u.Username); } // Fetch the Role ID for 'Supplier Group Account Partner User' Id partnerRoleId; try { partnerRoleId = [SELECT Id FROM UserRole WHERE Name = 'Supplier Group Account Partner User' LIMIT 1].Id; } catch (Exception e) { System.debug('⚠️ Role "Supplier Group Account Partner User" not found. Please check the name.'); } // Get correct Profile ID Id partnerProfileId; try { partnerProfileId = [SELECT Id FROM Profile WHERE Name = 'Partner Community User' LIMIT 1].Id; } catch (Exception e) { System.debug('⚠️ Profile "Partner Community User" not found. Please check the name.'); } // Prepare list of users to insert List usersToInsert = new List(); List duplicateUsernames = new List(); for (Contact con : recentContacts) { if (con.Email != null && !existingPartnerUserContacts.contains(con.Id)) { String generatedUsername = con.Email + '.scar'; // ✅ Skip duplicates and insert only unique usernames if (existingUsernames.contains(generatedUsername)) { duplicateUsernames.add(generatedUsername); System.debug('⚠️ Duplicate Username Found: ' + generatedUsername); continue; // Skip this user and move to the next one } // Get location data based on email Map locationData = emailToLocationMap.containsKey(con.Email) ? emailToLocationMap.get(con.Email) : null; User newUser = new User( FirstName = 'SCAR '+con.FirstName, LastName = con.LastName, Username = generatedUsername, Email = con.Email, Alias = con.LastName.length() > 5 ? con.LastName.substring(0, 5) : con.LastName, ProfileId = partnerProfileId, UserRoleId = partnerRoleId, TimeZoneSidKey = locationData != null ? locationData.get('TimeZone') : 'Asia/Hong_Kong', LocaleSidKey = 'en_US', EmailEncodingKey = 'UTF-8', LanguageLocaleKey = 'en_US', IsActive = true, ContactId = con.Id, Department = 'Supplier', FederationIdentifier = con.Email, Country = locationData != null ? locationData.get('Country') : 'China', City = locationData != null ? locationData.get('City') : 'Unknown' ); usersToInsert.add(newUser); existingUsernames.add(generatedUsername); // ✅ Add username to Set immediately } } // ✅ Insert all unique users and handle errors if (!usersToInsert.isEmpty()) { try { insert usersToInsert; } catch (DmlException e) { System.debug('❌ Error inserting users: ' + e.getMessage()); } }