|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Certinia Inc. All rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +public with sharing class AccountService { |
| 6 | + |
| 7 | + static Date KNOWN_MONDAY = Date.newInstance(2025, 6, 30); |
| 8 | + |
| 9 | + public static void createAccountsAndContacts() { |
| 10 | + |
| 11 | + Decimal revenue = getRevenue(); |
| 12 | + |
| 13 | + List<Account> accountsToInsert = new List<Account>(); |
| 14 | + for (Integer i = 0; i < 300; i++) { |
| 15 | + Account acc = new Account(); |
| 16 | + acc.Name = 'Account ' + i; |
| 17 | + acc.Description = 'This is account number ' + i; |
| 18 | + acc.AnnualRevenue = 15000; |
| 19 | + accountsToInsert.add(acc); |
| 20 | + } |
| 21 | + |
| 22 | + System.debug('Accounts to insert: ' + accountsToInsert); |
| 23 | + |
| 24 | + // Validate Accounts |
| 25 | + for (Account acc : accountsToInsert) { |
| 26 | + if (String.isBlank(acc.Name)) { |
| 27 | + throw new IllegalArgumentException('Account Name cannot be blank.'); |
| 28 | + } |
| 29 | + if (acc.AnnualRevenue != null && acc.AnnualRevenue < 0) { |
| 30 | + throw new IllegalArgumentException('Annual Revenue cannot be negative.'); |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + // Insert Accounts |
| 35 | + try { |
| 36 | + insert accountsToInsert; |
| 37 | + System.debug('Accounts inserted successfully.'); |
| 38 | + } catch (DmlException e) { |
| 39 | + System.debug('Error inserting accounts: ' + e.getMessage()); |
| 40 | + throw e; |
| 41 | + } |
| 42 | + |
| 43 | + // Map Account Names to IDs for linking Contacts |
| 44 | + Map<String, Id> accountNameToIdMap = new Map<String, Id>(); |
| 45 | + for (Account acc : accountsToInsert) { |
| 46 | + accountNameToIdMap.put(acc.Name, acc.Id); |
| 47 | + } |
| 48 | + |
| 49 | + List<Contact> contacts = new List<Contact>(); |
| 50 | + for (Account acc : accountsToInsert) { |
| 51 | + Contact contact = new Contact( |
| 52 | + FirstName = 'Linked', |
| 53 | + LastName = 'Contact ' + acc.Name, |
| 54 | + AccountId = acc.Id |
| 55 | + ); |
| 56 | + contacts.add(contact); |
| 57 | + } |
| 58 | + |
| 59 | + System.debug('Contacts to insert: ' + contacts); |
| 60 | + |
| 61 | + // Validate and link Contacts |
| 62 | + for (Contact con : contacts) { |
| 63 | + if (String.isBlank(con.LastName)) { |
| 64 | + throw new IllegalArgumentException('Contact Last Name cannot be blank.'); |
| 65 | + } |
| 66 | + |
| 67 | + if (String.isBlank(con.AccountId)){ |
| 68 | + throw new IllegalArgumentException('Contact must be linked to a valid Account.'); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Query Accounts for additional validation |
| 73 | + Set<Id> accountIds = new Set<Id>(); |
| 74 | + for (Contact con : contacts) { |
| 75 | + accountIds.add(con.AccountId); |
| 76 | + } |
| 77 | + accountIds.remove(null); |
| 78 | + |
| 79 | + Map<Id, Account> accountById = new Map<Id, Account>([SELECT Id, Name, AnnualRevenue FROM Account WHERE Id IN :accountIds]); |
| 80 | + System.debug('Queried Accounts: ' + accountById); |
| 81 | + |
| 82 | + // Perform additional validation based on queried Accounts |
| 83 | + for (Contact con : contacts) { |
| 84 | + Account relatedAccount = getAccount(con.AccountId, accountById); |
| 85 | + if (relatedAccount != null && relatedAccount.AnnualRevenue < 10000) { |
| 86 | + throw new IllegalArgumentException('Contact cannot be linked to an Account with Annual Revenue less than 10,000.'); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + new RecursiveSearcher().search(15, 15, 1100); |
| 91 | + |
| 92 | + // Insert Contacts |
| 93 | + try { |
| 94 | + insert contacts; |
| 95 | + System.debug('Contacts inserted successfully.'); |
| 96 | + } catch (DmlException e) { |
| 97 | + System.debug('Error inserting contacts: ' + e.getMessage()); |
| 98 | + throw e; |
| 99 | + } |
| 100 | + |
| 101 | + System.debug('Account and Contact creation completed successfully.'); |
| 102 | + new RecursiveSearcher().search(4, 8, 500); |
| 103 | + // LogUtil.burnLogStatements(7000); |
| 104 | + // LogUtil.burnCPU(500); |
| 105 | + } |
| 106 | + |
| 107 | + private static Account getAccount(Id accountId, Map<Id, Account> accountById){ |
| 108 | + return accountById.get(accountId); |
| 109 | + // NOTE: Purposly using a loop and not a map to create a more dense debug log |
| 110 | + // for(Account a : accounts){ |
| 111 | + // if(a.Id == accountId) { |
| 112 | + // return a; |
| 113 | + // } |
| 114 | + // } |
| 115 | + // return null; |
| 116 | + } |
| 117 | + |
| 118 | + private static Decimal getRevenue() { |
| 119 | + |
| 120 | + return getDayValue(Date.today())* 10; |
| 121 | + // Date tdy = Date.today(); |
| 122 | + // Date startOfYear = Date.newInstance(tdy.year(), 1, 1); |
| 123 | + // Date endOfYear = Date.newInstance(tdy.year(), 12, 31); |
| 124 | + |
| 125 | + |
| 126 | + // Decimal revenue = 0 ; |
| 127 | + // while(startOfYear < tdy){ |
| 128 | + // revenue += getDayValue(startOfYear) * 2; |
| 129 | + // startOfYear.addDays(1); |
| 130 | + // } |
| 131 | + |
| 132 | + // while(endOfYear > tdy){ |
| 133 | + // revenue += getDayValue(endOfYear); |
| 134 | + // endOfYear.addDays(-1); |
| 135 | + // } |
| 136 | + |
| 137 | + // return revenue; |
| 138 | + |
| 139 | + } |
| 140 | + |
| 141 | + private static Decimal getDayValue(Date day) { |
| 142 | + Integer dayDiff = KNOWN_MONDAY.daysBetween(day); |
| 143 | + Integer dayIndex = Math.mod(dayDiff, 7); |
| 144 | + switch on dayIndex { |
| 145 | + when 0, 1, 2, 3, 4 { |
| 146 | + return 2000; |
| 147 | + } |
| 148 | + when 5 { |
| 149 | + return 1000; |
| 150 | + } |
| 151 | + when else { |
| 152 | + return 0; |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + |
| 158 | + |
| 159 | +} |
0 commit comments