-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAccountService.cls
56 lines (47 loc) · 1.87 KB
/
AccountService.cls
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
public with sharing class AccountService {
private DAO daoIns = DAO.getInstance();
public List<Contact> copyAddressToContacts(List<Account> updatedAccounts) {
SOQLBuilder builder = new SOQLBuilder(Contact.SObjectType)
.selectFields(new List<String>{ 'AccountId' })
.setWhereClause('AccountId = :updatedAccounts')
.setBindVariable('updatedAccounts', updatedAccounts);
List<Contact> childContacts = this.daoIns.doQuery(
builder.getQuery(),
builder.getBinds(),
AccessLevel.USER_MODE
);
Map<Id, List<Contact>> accountIdToContacts = new Map<Id, List<Contact>>();
for (Contact childContact : childContacts) {
if (accountIdToContacts.containsKey(childContact.AccountId)) {
accountIdToContacts.get(childContact.AccountId).add(childContact);
continue;
}
accountIdToContacts.put(
childContact.AccountId,
new List<Contact>{ childContact }
);
}
List<Contact> contactsToUpdate = new List<Contact>();
for (Account updatedAccount : updatedAccounts) {
if (!accountIdToContacts.containsKey(updatedAccount.Id)) {
continue;
}
for (Contact childContact : accountIdToContacts.get(updatedAccount.Id)) {
copyAddress(childContact, updatedAccount);
contactsToUpdate.add(childContact);
}
}
if (!contactsToUpdate.isEmpty()) {
this.daoIns.doUpdate(contactsToUpdate, Contact.SObjectType);
return contactsToUpdate;
}
return null;
}
private void copyAddress(Contact childContact, Account updatedAccount) {
childContact.OtherStreet = updatedAccount.ShippingStreet;
childContact.OtherCity = updatedAccount.ShippingCity;
childContact.OtherState = updatedAccount.ShippingState;
childContact.OtherCountry = updatedAccount.ShippingCountry;
childContact.OtherPostalCode = updatedAccount.ShippingPostalCode;
}
}