
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
trigger UpdateZipCode on Account (before insert, before update) {
list<string> zipcodeList = new List<string>();
string zipCode;
for (integer i = 0; i <trigger.new.size(); i++) {
zipcode = trigger.new[i].billingpostalCode;
system.debug('ZipCode: ' + zipcode);
if (zipcode != null) {
integer index = zipcode.indexof('-',0);
if (index != -1) {
zipcode = zipcode.substring(0, index);
}
system.debug('ZipCode after the filter: ' + zipcode);
zipcodeList.add(zipCode);
}
}
system.debug('zipcode size: ' + zipcodelist.size());
if (zipcodelist.size() > 0) {
List<Zip_Code_Table__c> zipCodeTableList = [Select id, name from Zip_Code_Table__c where name in :zipcodeList];
system.debug('Data returned by zipcodeTable : ' + zipCodeTableList.size());
if (zipCodeTableList.size() > 0) {
//Add the zip code into a map
map<string, id> mapZipName= new map<string, id>();
for (integer i=0; i<zipcodeTableList.size(); i++) {
mapZipName.put(zipcodeTableList[i].name, zipcodeTableList[i].id);
}
//There is possibility that the number of zipcode from account is more than the zipcodeid return from zipcode table
id zipcodeid;
for (integer i = 0; i <trigger.new.size(); i++) {
zipcode = trigger.new[i].billingpostalCode;
if (zipcode != null) {
integer index = zipcode.indexof('-',0);
if (index != -1) {
zipcode = zipcode.substring(0, index);
}
zipcodeid = mapZipName.get(zipcode);
system.debug('zipcodeid: ' + zipcodeid);
Trigger.new[i].Zip_Code__c = zipcodeid;
}
}
}
}
}
2 respostas
This is best asked in the developer forums.
However, the first part of the trigger is removing the zip code extension. So if the Account has a billing zip code of 43081-1234, it is removing the -1234 and adding 43081 into a list of strings (ZipCodeList). It is then querying Zip_Code_Table__c for the records for those zip codes. It looks like you have a zip code lookup field on the Account called Zip_Code__c. The trigger is populating that field with the Zip_Code__c record that matches the Account billing zip code.