-14 Removals
+41 Additions
1public with sharing class ZipAndSubDivisionPopulationWorkflow implements TriggerWorkflow 1public with sharing class ZipAndSubDivisionPopulationWorkflow implements TriggerWorkflow
2{ 2{
3 List<Merchant_Addresses__c> newMAList, oldMAList; 3 List<Merchant_Addresses__c> newMAList, oldMAList;
4 List<Address__c> newRAList, oldRAList; 4 List<Address__c> newRAList, oldRAList;
5 Boolean isBeforeForMA, isAfterForMA, isBeforeForRA, isAfterForRA; 5 Boolean isBeforeForMA, isAfterForMA, isBeforeForRA, isAfterForRA;
6 6
7 Boolean isMATrigger, isRATrigger; 7 Boolean isMATrigger, isRATrigger;
8 8
9 public ZipAndSubDivisionPopulationWorkflow() 9 public ZipAndSubDivisionPopulationWorkflow()
10 { 10 {
11 List<Sobject> triggerRecords = Trigger.New; 11 List<Sobject> triggerRecords = Trigger.New;
12 if(triggerRecords != null && triggerRecords.size() > 0) 12 if(triggerRecords != null && triggerRecords.size() > 0)
13 { 13 {
14 if(triggerRecords[0].getSObjectType() == Merchant_Addresses__c.sObjectType) 14 if(triggerRecords[0].getSObjectType() == Merchant_Addresses__c.sObjectType)
15 { 15 {
16 this((List<Merchant_Addresses__c>)Trigger.New, (List<Merchant_Addresses__c>)Trigger.Old, Trigger.isBefore, Trigger.isAfter); 16 this((List<Merchant_Addresses__c>)Trigger.New, (List<Merchant_Addresses__c>)Trigger.Old, Trigger.isBefore, Trigger.isAfter);
17 } 17 }
18 else if(triggerRecords[0].getSObjectType() == Address__c.sObjectType) 18 else if(triggerRecords[0].getSObjectType() == Address__c.sObjectType)
19 { 19 {
20 this((List<Address__c>)Trigger.New, (List<Address__c>)Trigger.Old, Trigger.isBefore, Trigger.isAfter); 20 this((List<Address__c>)Trigger.New, (List<Address__c>)Trigger.Old, Trigger.isBefore, Trigger.isAfter);
21 } 21 }
22 } 22 }
23 } 23 }
24 24
25 public ZipAndSubDivisionPopulationWorkflow(List<Merchant_Addresses__c> newList, List<Merchant_Addresses__c> oldList , Boolean isBefore, Boolean isAfter) 25 public ZipAndSubDivisionPopulationWorkflow(List<Merchant_Addresses__c> newList, List<Merchant_Addresses__c> oldList , Boolean isBefore, Boolean isAfter)
26 { 26 {
27 this.newMAList = newList; 27 this.newMAList = newList;
28 this.oldMAList = oldList; 28 this.oldMAList = oldList;
29 this.isBeforeForMA = isBefore; 29 this.isBeforeForMA = isBefore;
30 this.isAfterForMA = isAfter; 30 this.isAfterForMA = isAfter;
31 31
32 isMATrigger = true; 32 isMATrigger = true;
33 isRATrigger = false; 33 isRATrigger = false;
34 } 34 }
35 35
36 public ZipAndSubDivisionPopulationWorkflow(List<Address__c> newList, List<Address__c> oldList , Boolean isBefore, Boolean isAfter) 36 public ZipAndSubDivisionPopulationWorkflow(List<Address__c> newList, List<Address__c> oldList , Boolean isBefore, Boolean isAfter)
37 { 37 {
38 this.newRAList = newList; 38 this.newRAList = newList;
39 this.oldRAList = oldList; 39 this.oldRAList = oldList;
40 this.isBeforeForRA = isBefore; 40 this.isBeforeForRA = isBefore;
41 this.isAfterForRA = isAfter; 41 this.isAfterForRA = isAfter;
42 42
43 isMATrigger = false; 43 isMATrigger = false;
44 isRATrigger = true; 44 isRATrigger = true;
45 } 45 }
46 46
47 private static Boolean hasExecutedBeforeForMA{ get{if(hasExecutedBeforeForMA == null) hasExecutedBeforeForMA = false; return hasExecutedBeforeForMA;} private set;} 47 private static Boolean hasExecutedBeforeForMA{ get{if(hasExecutedBeforeForMA == null) hasExecutedBeforeForMA = false; return hasExecutedBeforeForMA;} private set;}
48 private static Boolean hasExecutedAfterForMA{ get{if(hasExecutedAfterForMA == null) hasExecutedAfterForMA = false; return hasExecutedAfterForMA;} private set;} 48 private static Boolean hasExecutedAfterForMA{ get{if(hasExecutedAfterForMA == null) hasExecutedAfterForMA = false; return hasExecutedAfterForMA;} private set;}
49 49
50 private static Boolean hasExecutedBeforeForRA{ get{if(hasExecutedBeforeForRA == null) hasExecutedBeforeForRA = false; return hasExecutedBeforeForRA;} private set;} 50 private static Boolean hasExecutedBeforeForRA{ get{if(hasExecutedBeforeForRA == null) hasExecutedBeforeForRA = false; return hasExecutedBeforeForRA;} private set;}
51 private static Boolean hasExecutedAfterForRA{ get{if(hasExecutedAfterForRA == null) hasExecutedAfterForRA = false; return hasExecutedAfterForRA;} private set;} 51 private static Boolean hasExecutedAfterForRA{ get{if(hasExecutedAfterForRA == null) hasExecutedAfterForRA = false; return hasExecutedAfterForRA;} private set;}
52 52
53 private static Boolean shouldUpdateAccountWithNewSubDivs{ get{if(shouldUpdateAccountWithNewSubDivs == null) shouldUpdateAccountWithNewSubDivs = false; return shouldUpdateAccountWithNewSubDivs;} private set;} 53 private static Boolean shouldUpdateAccountWithNewSubDivs{ get{if(shouldUpdateAccountWithNewSubDivs == null) shouldUpdateAccountWithNewSubDivs = false; return shouldUpdateAccountWithNewSubDivs;} private set;}
54 private static Boolean shouldUpdateOpportunityWithNewSubDivs{ get{if(shouldUpdateOpportunityWithNewSubDivs == null) shouldUpdateOpportunityWithNewSubDivs = false; return shouldUpdateOpportunityWithNewSubDivs;} private set;} 54 private static Boolean shouldUpdateOpportunityWithNewSubDivs{ get{if(shouldUpdateOpportunityWithNewSubDivs == null) shouldUpdateOpportunityWithNewSubDivs = false; return shouldUpdateOpportunityWithNewSubDivs;} private set;}
55 55
56 //business logic 56 //business logic
57 public void execute() 57 public void execute()
58 { 58 {
59 if(isMATrigger) 59 if(isMATrigger)
60 { 60 {
61 if(isBeforeForMA) 61 if(isBeforeForMA)
62 { 62 {
63 if(!hasExecutedBeforeForMA) 63 if(!hasExecutedBeforeForMA)
64 { 64 {
65 updateMerchantAddressesWithZipCodeAndSubDivisionReferences(); 65 updateMerchantAddressesWithZipCodeAndSubDivisionReferences();
66 hasExecutedBeforeForMA = true; 66 hasExecutedBeforeForMA = true;
67 } 67 }
68 } 68 }
69 else if (isAfterForMA && shouldUpdateAccountWithNewSubDivs) 69 else if (isAfterForMA && shouldUpdateAccountWithNewSubDivs)
70 { 70 {
71 if(!hasExecutedAfterForMA) 71 if(!hasExecutedAfterForMA)
72 { 72 {
73 //updateParentRecordsWithSubvisions(newMAList, oldMAList, 'Zip_Postal_Code__c', 'Subdivision', 'Account__c', 'Merchant_Addresses__c'); 73 //updateParentRecordsWithSubvisions(newMAList, oldMAList, 'Zip_Postal_Code__c', 'Subdivision', 'Account__c', 'Merchant_Addresses__c');
74 this.updateAccountWithSubvisions(); 74 this.updateAccountWithSubvisions();
75 hasExecutedAfterForMA = true; 75 hasExecutedAfterForMA = true;
76 } 76 }
77 } 77 }
78 } 78 }
79 else if(isRATrigger) 79 else if(isRATrigger)
80 { 80 {
81 if(isBeforeForRA) 81 if(isBeforeForRA)
82 { 82 {
83 if(!hasExecutedBeforeForRA) 83 if(!hasExecutedBeforeForRA)
84 { 84 {
85 updateRedemptionAddressesWithZipCodeAndSubDivisionReferences(); 85 updateRedemptionAddressesWithZipCodeAndSubDivisionReferences();
86 hasExecutedBeforeForMA = true; 86 hasExecutedBeforeForMA = true;
87 } 87 }
88 } 88 }
89 else if (isAfterForRA && shouldUpdateOpportunityWithNewSubDivs) 89 else if (isAfterForRA && shouldUpdateOpportunityWithNewSubDivs)
90 { 90 {
91 if(!hasExecutedAfterForRA) 91 if(!hasExecutedAfterForRA)
92 { 92 {
93 //updateParentRecordsWithSubvisions(newRAList, oldRAList, 'Zip_Postal_Code__c', 'Subdivision', 'Opportunity__c', 'Address__c'); 93 //updateParentRecordsWithSubvisions(newRAList, oldRAList, 'Zip_Postal_Code__c', 'Subdivision', 'Opportunity__c', 'Address__c');
94 this.updateOpportunitysWithSubvisions(); 94 this.updateOpportunitysWithSubvisions();
95 hasExecutedAfterForRA = true; 95 hasExecutedAfterForRA = true;
96 } 96 }
97 } 97 }
98 } 98 }
99 } 99 }
100 100
101 101
102 private void updateMerchantAddressesWithZipCodeAndSubDivisionReferences() 102 private void updateMerchantAddressesWithZipCodeAndSubDivisionReferences()
103 { 103 {
104 Map<String, List<Merchant_Addresses__c>> qualifiedMAsMappedByZipCodes = AddressServices.getQualifiedZipCodesToAddressObjectMap(newMAList, oldMAList, 'Zip_Postal_Code__c', 'State_Province__c'); 104 List<Merchant_Addresses__c> oldM=new List<Merchant_Addresses__c>();
105 List<Merchant_Addresses__c> newM= new List<Merchant_Addresses__c>();
106 for(Integer i=0;i<newMAList.size();i++){
107 if(newMaList[i].Country_Code__c=='US'||newMaList[i].Country_Code__c=='CA'||newMaList[i].Country_Code__c==''){
108 oldM.add(oldMAList[i]);
109 newM.add(newMAList[i]);
110 }
111 }
112 Map<String, List<Merchant_Addresses__c>> qualifiedMAsMappedByZipCodes = AddressServices.getQualifiedZipCodesToAddressObjectMap(newM, oldM, 'Zip_Postal_Code__c', 'State_Province__c');
105 if(qualifiedMAsMappedByZipCodes != null && qualifiedMAsMappedByZipCodes.size() > 0) 113 if(qualifiedMAsMappedByZipCodes != null && qualifiedMAsMappedByZipCodes.size() > 0)
106 { 114 {
107 AddressServices.assignZipCodeAndSubDivision(qualifiedMAsMappedByZipCodes, 'Zip_Code__c', 'Subdivision__c'); 115 AddressServices.assignZipCodeAndSubDivision(qualifiedMAsMappedByZipCodes, 'Zip_Code__c', 'Subdivision__c');
108 //accounts should only be updated in the "after" part of trigger if there actually was a zipcode update 116 //accounts should only be updated in the "after" part of trigger if there actually was a zipcode update
109 shouldUpdateAccountWithNewSubDivs = true; 117 shouldUpdateAccountWithNewSubDivs = true;
110 } 118 }
111 } 119 }
112 120
113 121
114 private void updateRedemptionAddressesWithZipCodeAndSubDivisionReferences() 122 private void updateRedemptionAddressesWithZipCodeAndSubDivisionReferences()
115 { 123 {
124 List<Address__c> oldM=new List<Address__c>();
125 List<Address__c> newM= new List<Address__c>();
126 for(Integer i=0;i<newRAList.size();i++){
127 if(newRAList[i].Country_Code__c=='US'||newRAList[i].Country_Code__c=='CA'||newRAList[i].Country_Code__c==''){
128 oldM.add(oldRAList[i]);
129 newM.add(newRAList[i]);
130 }
131 }
132
116 Map<String, List<Address__c>> qualifiedRAsMappedByZipCodes = AddressServices.getQualifiedZipCodesToAddressObjectMap(newRAList, oldRAList, 'Zip_Postal_Code__c', 'State__c'); 133 Map<String, List<Address__c>> qualifiedRAsMappedByZipCodes = AddressServices.getQualifiedZipCodesToAddressObjectMap(newRAList, oldRAList, 'Zip_Postal_Code__c', 'State__c');
117 if(qualifiedRAsMappedByZipCodes != null && qualifiedRAsMappedByZipCodes.size() > 0) 134 if(qualifiedRAsMappedByZipCodes != null && qualifiedRAsMappedByZipCodes.size() > 0)
118 { 135 {
119 AddressServices.assignZipCodeAndSubDivision(qualifiedRAsMappedByZipCodes, 'Dynamic_Zip_Code__c', 'Subdivision__c'); 136 AddressServices.assignZipCodeAndSubDivision(qualifiedRAsMappedByZipCodes, 'Dynamic_Zip_Code__c', 'Subdivision__c');
120 //opptys should only be updated in the "after" part of trigger if there actually was a zipcode update 137 //opptys should only be updated in the "after" part of trigger if there actually was a zipcode update
121 shouldUpdateOpportunityWithNewSubDivs = true; 138 shouldUpdateOpportunityWithNewSubDivs = true;
122 } 139 }
123 } 140 }
124 141
125 //this method queries all the Subdivisions underneath the account and accumulates them in the form of a semi colon delimited string of values to update the account 142 //this method queries all the Subdivisions underneath the account and accumulates them in the form of a semi colon delimited string of values to update the account
126 private void updateAccountWithSubvisions() 143 private void updateAccountWithSubvisions()
127 { 144 {
128 Set<ID> accountIDs = new Set<ID>(); 145 Set<ID> accountIDs = new Set<ID>();
129 Integer x = 0; 146 Integer x = 0;
130 for(Merchant_Addresses__c ma: newMAList) 147 for(Merchant_Addresses__c ma: newMAList)
131 { 148 {
132 if(oldMAList == null || (ma.Zip_Postal_Code__c != oldMAList[x].Zip_Postal_Code__c)) 149 if(oldMAList == null || (ma.Zip_Postal_Code__c != oldMAList[x].Zip_Postal_Code__c))
133 accountIDs.add(ma.Account__c); 150 accountIDs.add(ma.Account__c);
134 x++; 151 x++;
135 } 152 }
136 153
137 if(accountIDs.size() > 0) 154 if(accountIDs.size() > 0)
138 { 155 {
139 Map<ID, Set<String>> mapAccountIDToSubDivs = new Map<ID, Set<String>>(); 156 Map<ID, Set<String>> mapAccountIDToSubDivs = new Map<ID, Set<String>>();
140 for(Merchant_Addresses__c ma: [Select ID, Account__c, Subdivision__c, Subdivision__r.Name, Zip_Postal_Code__c from Merchant_Addresses__c where Account__c in : accountIDs]) 157 for(Merchant_Addresses__c ma: [Select ID, Account__c,Account__r.Feature_Country__c, Subdivision__c, Subdivision__r.Name, Zip_Postal_Code__c from Merchant_Addresses__c where Account__c in : accountIDs])
141 { 158 {
142 Set<String> subDivs = new Set<String>(); 159 if(ma.Account__r.Feature_Country__c=='US'||ma.Account__r.Feature_Country__c=='CA'||ma.Account__r.Feature_Country__c==''){
143 if(mapAccountIDToSubDivs.containsKey(ma.Account__c)) 160 Set<String> subDivs = new Set<String>();
144 subDivs = mapAccountIDToSubDivs.get(ma.Account__c); 161 if(mapAccountIDToSubDivs.containsKey(ma.Account__c))
145 162 subDivs = mapAccountIDToSubDivs.get(ma.Account__c);
146 if(ma.Subdivision__r.Name != null) 163
147 { 164 if(ma.Subdivision__r.Name != null)
148 subDivs.add(ma.Subdivision__r.Name); 165 {
149 mapAccountIDToSubDivs.put(ma.Account__c, subDivs); 166 subDivs.add(ma.Subdivision__r.Name);
167 mapAccountIDToSubDivs.put(ma.Account__c, subDivs);
168 }
150 } 169 }
151 } 170 }
152 171
153 if(mapAccountIDToSubDivs.size() > 0) 172 if(mapAccountIDToSubDivs.size() > 0)
154 { 173 {
155 List<Account> accountsToUpdate = new List<Account>(); 174 List<Account> accountsToUpdate = new List<Account>();
156 for(ID accID: mapAccountIDToSubDivs.keySet()) 175 for(ID accID: mapAccountIDToSubDivs.keySet())
157 { 176 {
158 Account a = new Account(ID = accID); 177 Account a = new Account(ID = accID);
159 if(mapAccountIDToSubDivs.get(accID).size() > 99) 178 if(mapAccountIDToSubDivs.get(accID).size() > 99)
160 a.Subdivision__c = 'National Account: Too many subdivisions'; 179 a.Subdivision__c = 'National Account: Too many subdivisions';
161 else 180 else
162 { 181 {
163 a.Subdivision__c = GrouponUtils.getCSVFromSet(mapAccountIDToSubDivs.get(accID), ';'); 182 a.Subdivision__c = GrouponUtils.getCSVFromSet(mapAccountIDToSubDivs.get(accID), ';');
164 accountsToUpdate.add(a); 183 accountsToUpdate.add(a);
165 } 184 }
166 } 185 }
167 186
168 if(accountsToUpdate.size() > 0) 187 if(accountsToUpdate.size() > 0)
169 update accountsToUpdate; 188 update accountsToUpdate;
170 } 189 }
171 } 190 }
172 191
173 } 192 }
174 193
175 //this method queries all the Subdivisions underneath the account and accumulates them in the form of a semi colon delimited string of values to update the account 194 //this method queries all the Subdivisions underneath the account and accumulates them in the form of a semi colon delimited string of values to update the account
176 private void updateOpportunitysWithSubvisions() 195 private void updateOpportunitysWithSubvisions()
177 { 196 {
178 Set<ID> opptyIDs = new Set<ID>(); 197 Set<ID> opptyIDs = new Set<ID>();
179 Integer x = 0; 198 Integer x = 0;
180 for(Address__c ra: newRAList) 199 for(Address__c ra: newRAList)
181 { 200 {
201 if(ra.Country_Code__c =='US'||ra.Country_Code__c =='CA'||ra.Country_Code__c==''){
182 if(oldRAList == null || (ra.Zip_Postal_Code__c != oldRAList[x].Zip_Postal_Code__c)) 202 if(oldRAList == null || (ra.Zip_Postal_Code__c != oldRAList[x].Zip_Postal_Code__c))
183 opptyIDs.add(ra.Opportunity__c); 203 opptyIDs.add(ra.Opportunity__c);
204 }
184 x++; 205 x++;
185 } 206 }
186 207
187 if(opptyIDs.size() > 0) 208 if(opptyIDs.size() > 0)
188 { 209 {
189 Map<ID, Set<String>> mapOpptyIDToSubDivs = new Map<ID, Set<String>>(); 210 Map<ID, Set<String>> mapOpptyIDToSubDivs = new Map<ID, Set<String>>();
190 for(Address__c ra: [Select ID, Opportunity__c, Subdivision__c, Subdivision__r.Name, Zip_Postal_Code__c from Address__c where Opportunity__c in : opptyIDs]) 211 for(Address__c ra: [Select ID, Opportunity__c, Subdivision__c, Subdivision__r.Name, Zip_Postal_Code__c from Address__c where Opportunity__c in : opptyIDs])
191 { 212 {
192 Set<String> subDivs = new Set<String>(); 213 Set<String> subDivs = new Set<String>();
193 if(mapOpptyIDToSubDivs.containsKey(ra.Opportunity__c)) 214 if(mapOpptyIDToSubDivs.containsKey(ra.Opportunity__c))
194 subDivs = mapOpptyIDToSubDivs.get(ra.Opportunity__c); 215 subDivs = mapOpptyIDToSubDivs.get(ra.Opportunity__c);
195 216 if(ra.Country_Code__c =='US'||ra.Country_Code__c =='CA'||ra.Country_Code__c==''){
196 if(ra.Subdivision__r.Name != null) 217 if(ra.Subdivision__r.Name != null)
197 { 218 {
198 subDivs.add(ra.Subdivision__r.Name); 219 subDivs.add(ra.Subdivision__r.Name);
199 mapOpptyIDToSubDivs.put(ra.Opportunity__c, subDivs); 220 mapOpptyIDToSubDivs.put(ra.Opportunity__c, subDivs);
221 }
200 } 222 }
201 } 223 }
202 System.debug('\n\nmapOpptyIDToSubDivs = '+mapOpptyIDToSubDivs+'\n\n'); 224 System.debug('\n\nmapOpptyIDToSubDivs = '+mapOpptyIDToSubDivs+'\n\n');
203 225
204 if(mapOpptyIDToSubDivs.size() > 0) 226 if(mapOpptyIDToSubDivs.size() > 0)
205 { 227 {
206 List<Opportunity> opptysToUpdate = new List<Opportunity>(); 228 List<Opportunity> opptysToUpdate = new List<Opportunity>();
207 for(ID opptyID: mapOpptyIDToSubDivs.keySet()) 229 for(ID opptyID: mapOpptyIDToSubDivs.keySet())
208 { 230 {
209 Opportunity oppty = new Opportunity(ID = opptyID); 231 Opportunity oppty = new Opportunity(ID = opptyID);
210 if(mapOpptyIDToSubDivs.get(opptyID).size() > 99) 232 if(mapOpptyIDToSubDivs.get(opptyID).size() > 99)
211 oppty.Subdivision__c = 'National Account: Too many subdivisions'; 233 oppty.Subdivision__c = 'National Account: Too many subdivisions';
212 else 234 else
213 { 235 {
214 oppty.Subdivision__c = GrouponUtils.getCSVFromSet(mapOpptyIDToSubDivs.get(opptyID), ';'); 236 oppty.Subdivision__c = GrouponUtils.getCSVFromSet(mapOpptyIDToSubDivs.get(opptyID), ';');
215 opptysToUpdate.add(oppty); 237 opptysToUpdate.add(oppty);
216 } 238 }
217 } 239 }
218 240
219 if(opptysToUpdate.size() > 0) 241 if(opptysToUpdate.size() > 0)
220 update opptysToUpdate; 242 update opptysToUpdate;
221 } 243 }
222 } 244 }
223 245
224 } 246 }
225 247
226 /* 248 /*
227 //this method queries all the Subdivisions underneath the account and accumulates them in the form of a semi colon delimited string of values to update the account 249 //this method queries all the Subdivisions underneath the account and accumulates them in the form of a semi colon delimited string of values to update the account
228 private void updateParentRecordsWithSubvisions(List<SObject> newList, List<SObject> oldList, String zipFieldName, String subdivisionFieldName, String parentReferenceFieldName ,String SObjectName ) 250 private void updateParentRecordsWithSubvisions(List<SObject> newList, List<SObject> oldList, String zipFieldName, String subdivisionFieldName, String parentReferenceFieldName ,String SObjectName )
229 { 251 {
230 Set<String> parentIDs = new Set<String>(); 252 Set<String> parentIDs = new Set<String>();
231 Integer x = 0; 253 Integer x = 0;
232 for(SObject addressRecord: newList) 254 for(SObject addressRecord: newList)
233 { 255 {
234 if(oldList == null || (addressRecord.get(zipFieldName) != oldList[x].get(zipFieldName))) 256 if(oldList == null || (addressRecord.get(zipFieldName) != oldList[x].get(zipFieldName)))
235 parentIDs.add(String.valueOf(addressRecord.get(parentReferenceFieldName))); 257 parentIDs.add(String.valueOf(addressRecord.get(parentReferenceFieldName)));
236 258
237 x++; 259 x++;
238 } 260 }
239 261
240 if(parentIDs.size() > 0) 262 if(parentIDs.size() > 0)
241 { 263 {
242 Map<String, Set<String>> mapParentIDToSubDivs = new Map<String, Set<String>>(); 264 Map<String, Set<String>> mapParentIDToSubDivs = new Map<String, Set<String>>();
243 String query = 'Select ID, '+SubdivisionFieldName+'__c, '+SubdivisionFieldName+'__r.Name, '+parentReferenceFieldName+' from '+SobjectName+' where '+parentReferenceFieldName+' in: parentIDs'; 265 String query = 'Select ID, '+SubdivisionFieldName+'__c, '+SubdivisionFieldName+'__r.Name, '+parentReferenceFieldName+' from '+SobjectName+' where '+parentReferenceFieldName+' in: parentIDs';
244 System.debug('\n\nquery = '+query+'\n\n'); 266 System.debug('\n\nquery = '+query+'\n\n');
245 for(SObject sObj: Database.query(query)) 267 for(SObject sObj: Database.query(query))
246 { 268 {
247 Set<String> subDivs = new Set<String>(); 269 Set<String> subDivs = new Set<String>();
248 if(mapParentIDToSubDivs.containsKey(String.valueOf(sObj.get(parentReferenceFieldName)))) 270 if(mapParentIDToSubDivs.containsKey(String.valueOf(sObj.get(parentReferenceFieldName))))
249 subDivs = mapParentIDToSubDivs.get(String.valueOf(sObj.get(parentReferenceFieldName))); 271 subDivs = mapParentIDToSubDivs.get(String.valueOf(sObj.get(parentReferenceFieldName)));
250 272
251 if(sObj.get(subdivisionFieldName+'__r.Name') != null) 273 if(sObj.get(subdivisionFieldName+'__r.Name') != null)
252 { 274 {
253 subDivs.add(String.valueOf(sObj.get(subdivisionFieldName+'__r.Name'))); 275 subDivs.add(String.valueOf(sObj.get(subdivisionFieldName+'__r.Name')));
254 mapParentIDToSubDivs.put(String.valueOf(sObj.get(parentReferenceFieldName)), subDivs); 276 mapParentIDToSubDivs.put(String.valueOf(sObj.get(parentReferenceFieldName)), subDivs);
255 } 277 }
256 } 278 }
257 //System.debug('\n\nmapOpptyIDToSubDivs = '+mapOpptyIDToSubDivs+'\n\n'); 279 //System.debug('\n\nmapOpptyIDToSubDivs = '+mapOpptyIDToSubDivs+'\n\n');
258 280
259 if(mapParentIDToSubDivs.size() > 0) 281 if(mapParentIDToSubDivs.size() > 0)
260 { 282 {
261 List<SObject> parentsToUpdate = new List<SObject>(); 283 List<SObject> parentsToUpdate = new List<SObject>();
262 for(String parentID: mapParentIDToSubDivs.keySet()) 284 for(String parentID: mapParentIDToSubDivs.keySet())
263 { 285 {
264 SObject parentRecord; 286 SObject parentRecord;
265 if(isMATrigger) 287 if(isMATrigger)
266 parentRecord = new Account(ID = parentID); 288 parentRecord = new Account(ID = parentID);
267 else if(isRATrigger) 289 else if(isRATrigger)
268 parentRecord = new Opportunity(ID = parentID); 290 parentRecord = new Opportunity(ID = parentID);
269 291
270 if(mapParentIDToSubDivs.get(parentID).size() > 99) 292 if(mapParentIDToSubDivs.get(parentID).size() > 99)
271 parentRecord.put(subdivisionFieldName+'__c', 'National Account: Too many subdivisions'); 293 parentRecord.put(subdivisionFieldName+'__c', 'National Account: Too many subdivisions');
272 else 294 else
273 { 295 {
274 parentRecord.put(subdivisionFieldName+'__c', GrouponUtils.getCSVFromSet(mapParentIDToSubDivs.get(parentID), ';')); 296 parentRecord.put(subdivisionFieldName+'__c', GrouponUtils.getCSVFromSet(mapParentIDToSubDivs.get(parentID), ';'));
275 parentsToUpdate.add(parentRecord); 297 parentsToUpdate.add(parentRecord);
276 } 298 }
277 } 299 }
278 300
279 if(parentsToUpdate.size() > 0) 301 if(parentsToUpdate.size() > 0)
280 update parentsToUpdate; 302 update parentsToUpdate;
281 } 303 }
282 } 304 }
283 305
284 } 306 }
285 */ 307 */
286 308
287}309}
Editor
Original Text
Changed Text
Recommended videos