You need to sign in to do that
Don't have an account?

Need help woth a trigger
As I am not an Apex guy, I need some help. I have this trigger that I inheritied, but it only works for US zip code. I need to eithr modify it or add another trigger that will work for Canadian zip codes. We have a custom object called zip code table that lists all Us zip code by 5 digit, but canadian zip codes by first 3 digits (that is where this breaks down, I bleieve)
Can anyone assist>
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;
}
}
}
}
}
Can anyone assist>
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;
}
}
}
}
}
In zip code table, is keeping below zip code.
US : 00001 zipcodeID1
CAN: 221 zipcodeId2
In billingpostalCode will have US values like 00001-22, 00001-33,00001, this part works fine.
But for CAN does not working, I will guess it's because the billingpostalcode for CAN entered are not just 221,221-11,221-3, it must be like 221 followed with some other character like 221~,221/,221 - (221space-) etc.Thus, trigger will not able to get correct zipcode of 221, it will searching 221~ or 221/ or 221 (221plus a space) in zip code table, but definatly will return nothing.
So either update this piece of code to pharse CAN zipcode format, or enter CAN zipcode in billingpostalcode field with strictly correct format of XXX-XX.
Hope this help.
when you insert/update a account having billingpostalcode with CAN zip code, could you paste the debug log here?
9.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO
08:57:36.063 (63766000)|ENTERING_MANAGED_PKG|SFSSDupeCatcher
08:57:36.093 (93379000)|SOQL_EXECUTE_BEGIN|[722]|Aggregations:0|select Name, CreatedDate, Id from Override_Cache__c where Session_Id__c = :tmpVar1 limit 50
08:57:36.097 (97187000)|SOQL_EXECUTE_END|[722]|Rows:0
08:57:36.099 (99495000)|SOQL_EXECUTE_BEGIN|[722]|Aggregations:0|select Name, CreatedDate, Id from Override_Cache__c where Session_Id__c = :tmpVar1 limit 50
08:57:36.102 (102525000)|SOQL_EXECUTE_END|[722]|Rows:0
08:57:36.107 (107119000)|SOQL_EXECUTE_BEGIN|[159]|Aggregations:0|select Id, Name, SFSSDupeCatcher__Match_On_Insert_Action__c, SFSSDupeCatcher__Match_On_Update_Action__c, SFSSDupeCatcher__Create_Tasks_for_Warnings__c, SFSSDupeCatcher__Blocked_Duplicates__c, SFSSDupeCatcher__Merged_Duplicates__c, SFSSDupeCatcher__Converted_Duplicates__c, SFSSDupeCatcher__Error_Message__c, SFSSDupeCatcher__Scenario_Type__c, SFSSDupeCatcher__Deployed__c, SFSSDupeCatcher__Bypass_Security__c, SFSSDupeCatcher__Person_Account_Filter__c, OwnerID, CreatedById from Scenario__c where (Deployed__c = true and Match_On_Update_Action__c != 'Report Duplicate') limit 100
08:57:36.114 (114281000)|SOQL_EXECUTE_END|[159]|Rows:0
08:57:36.122 (122205000)|CODE_UNIT_STARTED|[EXTERNAL]|01q000000008Tns|UpdateZipCode on Account trigger event BeforeUpdate for [00100000006kRmI]
08:57:36.122 (122477000)|SYSTEM_CONSTRUCTOR_ENTRY|[2]|<init>()
08:57:36.122 (122526000)|SYSTEM_CONSTRUCTOR_EXIT|[2]|<init>()
08:57:36.122 (122588000)|SYSTEM_METHOD_ENTRY|[6]|LIST<Account>.size()
08:57:36.122 (122639000)|SYSTEM_METHOD_EXIT|[6]|LIST<Account>.size()
08:57:36.122 (122766000)|SYSTEM_METHOD_ENTRY|[8]|System.debug(ANY)
08:57:36.122 (122803000)|USER_DEBUG|[8]|DEBUG|ZipCode: v2n 3r1
08:57:36.122 (122817000)|SYSTEM_METHOD_EXIT|[8]|System.debug(ANY)
08:57:36.122 (122865000)|SYSTEM_METHOD_ENTRY|[16]|System.debug(ANY)
08:57:36.122 (122894000)|USER_DEBUG|[16]|DEBUG|ZipCode after the filter: v2n 3r1
08:57:36.122 (122908000)|SYSTEM_METHOD_EXIT|[16]|System.debug(ANY)
08:57:36.122 (122948000)|SYSTEM_METHOD_ENTRY|[17]|LIST<String>.add(Object)
08:57:36.123 (123000000)|SYSTEM_METHOD_EXIT|[17]|LIST<String>.add(Object)
08:57:36.123 (123031000)|SYSTEM_METHOD_ENTRY|[6]|LIST<Account>.size()
08:57:36.123 (123049000)|SYSTEM_METHOD_EXIT|[6]|LIST<Account>.size()
08:57:36.123 (123080000)|SYSTEM_METHOD_ENTRY|[20]|LIST<String>.size()
08:57:36.123 (123122000)|SYSTEM_METHOD_EXIT|[20]|LIST<String>.size()
08:57:36.123 (123145000)|SYSTEM_METHOD_ENTRY|[20]|String.valueOf(Object)
08:57:36.123 (123164000)|SYSTEM_METHOD_EXIT|[20]|String.valueOf(Object)
08:57:36.123 (123181000)|SYSTEM_METHOD_ENTRY|[20]|System.debug(ANY)
08:57:36.123 (123191000)|USER_DEBUG|[20]|DEBUG|zipcode size: 1
08:57:36.123 (123203000)|SYSTEM_METHOD_EXIT|[20]|System.debug(ANY)
08:57:36.123 (123229000)|SYSTEM_METHOD_ENTRY|[22]|LIST<String>.size()
08:57:36.123 (123280000)|SYSTEM_METHOD_EXIT|[22]|LIST<String>.size()
08:57:36.123 (123884000)|SOQL_EXECUTE_BEGIN|[23]|Aggregations:0|select id, name from Zip_Code_Table__c where name IN :tmpVar1
08:57:36.129 (129734000)|SOQL_EXECUTE_END|[23]|Rows:0
08:57:36.129 (129927000)|SYSTEM_METHOD_ENTRY|[24]|LIST<Zip_Code_Table__c>.size()
08:57:36.129 (129982000)|SYSTEM_METHOD_EXIT|[24]|LIST<Zip_Code_Table__c>.size()
08:57:36.130 (130005000)|SYSTEM_METHOD_ENTRY|[24]|String.valueOf(Object)
08:57:36.130 (130030000)|SYSTEM_METHOD_EXIT|[24]|String.valueOf(Object)
08:57:36.130 (130050000)|SYSTEM_METHOD_ENTRY|[24]|System.debug(ANY)
08:57:36.130 (130062000)|USER_DEBUG|[24]|DEBUG|Data returned by zipcodeTable : 0
08:57:36.130 (130074000)|SYSTEM_METHOD_EXIT|[24]|System.debug(ANY)
08:57:36.130 (130093000)|SYSTEM_METHOD_ENTRY|[27]|LIST<Zip_Code_Table__c>.size()
08:57:36.130 (130136000)|SYSTEM_METHOD_EXIT|[27]|LIST<Zip_Code_Table__c>.size()
08:57:36.911 (130165000)|CUMULATIVE_LIMIT_USAGE
08:57:36.911|LIMIT_USAGE_FOR_NS|(default)|
Number of SOQL queries: 1 out of 100
Number of query rows: 0 out of 50000
Number of SOSL queries: 0 out of 20
Number of DML statements: 0 out of 150
Number of DML rows: 0 out of 10000
Number of code statements: 0 out of 200000
Maximum CPU time: 0 out of 10000
Maximum heap size: 0 out of 6000000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 100
Number of record type describes: 0 out of 100
Number of child relationships describes: 0 out of 100
Number of picklist describes: 0 out of 100
Number of future calls: 0 out of 10
08:57:36.911|LIMIT_USAGE_FOR_NS|SFSSDupeCatcher|
Number of SOQL queries: 3 out of 100
Number of query rows: 0 out of 50000
Number of SOSL queries: 0 out of 20
Number of DML statements: 0 out of 150
Number of DML rows: 0 out of 10000
Number of code statements: 0 out of 200000
Maximum CPU time: 0 out of 10000
Maximum heap size: 0 out of 6000000
Number of callouts: 0 out of 10
Number of Email Invocations: 0 out of 10
Number of fields describes: 0 out of 100
Number of record type describes: 0 out of 100
Number of child relationships describes: 0 out of 100
Number of picklist describes: 0 out of 100
Number of future calls: 0 out of 10
08:57:36.911|CUMULATIVE_LIMIT_USAGE_END
08:57:36.130 (130349000)|CODE_UNIT_FINISHED|UpdateZipCode on Account trigger event BeforeUpdate for [00100000006kRmI]
08:57:36.134 (134858000)|ENTERING_MANAGED_PKG|i
08:57:36.142 (142774000)|ENTERING_MANAGED_PKG|i
08:57:36.155 (155782000)|CODE_UNIT_STARTED|[EXTERNAL]|Validation:Account:00100000006kRmI
08:57:36.155 (155807000)|VALIDATION_RULE|03d000000004CJr|Canadian_Billing_Province_Validation
08:57:36.155 (155999000)|VALIDATION_FORMULA|AND (
OR(BillingCountry = "CANADA", BillingCountry="Canada"),
OR(
LEN(BillingState) = 0,
NOT(
CONTAINS("AB:BC:MB:NB:NL:NT:NS:NU:ON:PC:QC:SK:YT:PE", BillingState)
))
)|BillingCountry=CANADA , BillingState=BC
08:57:36.156 (156008000)|VALIDATION_PASS
08:57:36.156 (156012000)|VALIDATION_RULE|03d000000004FA4|OCI_Canadian_Postal_Code_Validation
08:57:36.156 (156173000)|VALIDATION_FORMULA|AND(
OR(BillingCountry = "CAN", BillingCountry = "CANADA", BillingCountry = "Canada"),
NOT(REGEX(BillingPostalCode, "((?i)[ABCEGHJKLMNPRSTVXY]\\d[A-Z]?\\s?\\d[A-Z]\\d)?"))
)|BillingCountry=CANADA , BillingPostalCode=v2n 3r1
08:57:36.156 (156181000)|VALIDATION_PASS
08:57:36.156 (156184000)|VALIDATION_RULE|03d000000004PM1|Phone_Number_must_be_Numeric_Only
08:57:36.291 (291056000)|VALIDATION_FORMULA|And( $Profile.Name <>"System Administrator",OR(
CONTAINS( Phone , "a"),
CONTAINS(Phone , "b"),
CONTAINS(Phone , "c"),
CONTAINS(Phone , "d"),
CONTAINS(Phone , "e"),
CONTAINS(Phone , "f"),
CONTAINS(Phone , "g"),
CONTAINS(Phone , "h"),
CONTAINS(Phone , "i"),
CONTAINS(Phone , "j"),
CONTAINS(Phone , "k"),
CONTAINS(Phone , "l"),
CONTAINS(Phone , "m"),
CONTAINS(Phone , "n"),
CONTAINS(Phone , "o"),
CONTAINS(Phone , "p"),
CONTAINS(Phone , "q"),
CONTAINS(Phone , "r"),
CONTAINS(Phone , "s"),
CONTAINS(Phone , "t"),
CONTAINS(Phone , "u"),
CONTAINS(Phone , "v"),
CONTAINS(Phone , "x"),
CONTAINS(Phone , "y"),
CONTAINS(Phone , "z")))|Phone=(555) 555-1212 , $Profile.Name=System Administrator
08:57:36.291 (291076000)|VALIDATION_PASS
08:57:36.291 (291080000)|VALIDATION_RULE|03d000000004CwF|Service_Contract_Validation
08:57:36.291 (291211000)|VALIDATION_FORMULA|AND(
OR(ISNULL( Contract__c ), Contract__c = "" ),
( Service_Contract__c ))|Contract__c=null , Service_Contract__c=0
08:57:36.291 (291228000)|VALIDATION_PASS
08:57:36.291 (291232000)|VALIDATION_RULE|03d000000004FAA|USA_Zip_Code_Validation
08:57:36.291 (291371000)|VALIDATION_FORMULA|AND(
OR(BillingCountry = "USA", BillingCountry = "US"),
NOT(REGEX(BillingPostalCode, "\\d{5}(-\\d{4})?"))
)|BillingCountry=CANADA , BillingPostalCode=v2n 3r1
08:57:36.291 (291379000)|VALIDATION_PASS
08:57:36.291 (291382000)|VALIDATION_RULE|03d000000004cPi|Parent_Account_Required_for_Merge
08:57:36.291 (291476000)|VALIDATION_FORMULA|AND( LEN( ParentId ) = 0, Merge__c )|ParentId=null , Merge__c=0
08:57:36.291 (291487000)|VALIDATION_PASS
08:57:36.291 (291490000)|VALIDATION_RULE|03d000000000i41|Phone_number_must_be_valid
08:57:36.291 (291667000)|VALIDATION_FORMULA|AND(
OR(BillingCountry = "USA", BillingCountry = "US"),(OR(Phone = "(111) 111-1111",Phone ="(555) 555-5555",Phone ="(222) 222-2222",Phone = "1234567890",Phone ="(333) 333-3333",Phone ="(444) 444-4444",Phone ="(666) 666-6666",Phone ="(777) 777-7777",Phone ="(888) 888-8888")))|Phone=(555) 555-1212 , BillingCountry=CANADA
08:57:36.291 (291677000)|VALIDATION_PASS
08:57:36.291 (291683000)|VALIDATION_RULE|03d000000000i46|Phone_number_must_be_valid9
08:57:36.291 (291784000)|VALIDATION_FORMULA|AND(
OR(BillingCountry = "USA", BillingCountry = "US"),Phone ="(999) 999-9999")|Phone=(555) 555-1212 , BillingCountry=CANADA
08:57:36.291 (291791000)|VALIDATION_PASS
08:57:36.291 (291798000)|CODE_UNIT_FINISHED|Validation:Account:00100000006kRmI
08:57:36.393 (393397000)|ENTERING_MANAGED_PKG|SFSSDupeCatcher
08:57:36.395 (395124000)|SOQL_EXECUTE_BEGIN|[722]|Aggregations:0|select Name, CreatedDate, Id from Override_Cache__c where Session_Id__c = :tmpVar1 limit 50
08:57:36.398 (398038000)|SOQL_EXECUTE_END|[722]|Rows:0
08:57:36.400 (400030000)|SOQL_EXECUTE_BEGIN|[722]|Aggregations:0|select Name, CreatedDate, Id from Override_Cache__c where Session_Id__c = :tmpVar1 limit 50
08:57:36.403 (403099000)|SOQL_EXECUTE_END|[722]|Rows:0
08:57:36.405 (405467000)|SOQL_EXECUTE_BEGIN|[214]|Aggregations:0|select Id, Name, SFSSDupeCatcher__Match_On_Insert_Action__c, SFSSDupeCatcher__Match_On_Update_Action__c, SFSSDupeCatcher__Create_Tasks_for_Warnings__c, SFSSDupeCatcher__Blocked_Duplicates__c, SFSSDupeCatcher__Merged_Duplicates__c, SFSSDupeCatcher__Converted_Duplicates__c, SFSSDupeCatcher__Error_Message__c, SFSSDupeCatcher__Scenario_Type__c, SFSSDupeCatcher__Deployed__c, SFSSDupeCatcher__Bypass_Security__c, SFSSDupeCatcher__Person_Account_Filter__c, OwnerID, CreatedById from Scenario__c where Deployed__c = true limit 100
08:57:36.410 (410482000)|SOQL_EXECUTE_END|[214]|Rows:3
08:57:36.413 (413567000)|SOQL_EXECUTE_BEGIN|[34]|Aggregations:0|select Name, Id, SFSSDupeCatcher__Mapping_Approach__c, SFSSDupeCatcher__Match_field__c, SFSSDupeCatcher__Target_Match_Field__c, SFSSDupeCatcher__First_N_Letters__c, SFSSDupeCatcher__Match_Blanks__c, SFSSDupeCatcher__Scenario__c from Scenario_Rule__c limit :tmpVar1
08:57:36.419 (419539000)|SOQL_EXECUTE_END|[34]|Rows:6
08:57:36.435 (435253000)|SOQL_EXECUTE_BEGIN|[23]|Aggregations:0|select Name,Id,OwnerId from Account WHERE (Name like 'APPLE%') AND (Name = 'APPLE SERVICE') AND (Id != '00100000006kRmIAAU') Limit 15
08:57:36.446 (446693000)|SOQL_EXECUTE_END|[23]|Rows:0
08:57:36.454 (454993000)|ENTERING_MANAGED_PKG|i
08:57:36.459 (459081000)|ENTERING_MANAGED_PKG|CloudingoAgent
08:57:36.483 (483609000)|SOQL_EXECUTE_BEGIN|[65]|Aggregations:0|select Id, CloudingoAgent__EXID__c, CloudingoAgent__Content__c from SearchFilter__c limit 50000
08:57:36.489 (489944000)|SOQL_EXECUTE_END|[65]|Rows:12
08:57:36.491 (491411000)|SOQL_EXECUTE_BEGIN|[68]|Aggregations:0|select Id, Name, CloudingoAgent__ST__c from PO__c limit 50000
08:57:36.493 (493903000)|SOQL_EXECUTE_END|[68]|Rows:3
08:57:36.518 (518646000)|CODE_UNIT_STARTED|[EXTERNAL]|Workflow:Account
08:57:36.535 (535964000)|WF_RULE_EVAL_BEGIN|Workflow
08:57:36.536 (536001000)|WF_CRITERIA_BEGIN|[Account: APPLE SERVICE 00100000006kRmI]|Update email address from hidden|01Q000000009G2V|ON_ALL_CHANGES
08:57:36.538 (538987000)|WF_RULE_FILTER|[Account : HiddenEmailAddress not equal to null]
AND [Account : Billing Country equals USA, CANADA, CAN, US]
08:57:36.555 (555513000)|WF_RULE_EVAL_VALUE|rob.sepa@omron.com
08:57:36.555 (555532000)|WF_RULE_EVAL_VALUE|CANADA
08:57:36.555 (555544000)|WF_CRITERIA_END|true
08:57:36.556 (556792000)|WF_CRITERIA_BEGIN|[Account: APPLE SERVICE 00100000006kRmI]|Delete Account field checked|01Q000000009JeW|ON_CREATE_OR_TRIGGERING_UPDATE
08:57:36.556 (556848000)|WF_RULE_FILTER|[Account : Delete equals true]
08:57:36.556 (556856000)|WF_RULE_EVAL_VALUE|0
08:57:36.556 (556866000)|WF_CRITERIA_END|false
08:57:36.556 (556881000)|WF_CRITERIA_BEGIN|[Account: APPLE SERVICE 00100000006kRmI]|OEB Update the Mask of CNPJ - ACCOUNT|01Q000000009QcI|ON_ALL_CHANGES
08:57:36.556 (556909000)|WF_RULE_FILTER|[Account : Account Record Type equals OEB Customer Account]
08:57:36.556 (556920000)|WF_RULE_EVAL_VALUE|01200000000CfkZ
08:57:36.556 (556928000)|WF_CRITERIA_END|false
08:57:36.556 (556940000)|WF_CRITERIA_BEGIN|[Account: APPLE SERVICE 00100000006kRmI]|Update SF Source Fields|01Q000000009VdL|ON_CREATE_OR_TRIGGERING_UPDATE
08:57:36.568 (568889000)|WF_FORMULA|Formula:RecordType.DeveloperName = "OSTI_Customer_Account"|Values:RecordType.DeveloperName=AccountStandard
08:57:36.568 (568904000)|WF_CRITERIA_END|false
08:57:36.568 (568921000)|WF_CRITERIA_BEGIN|[Account: APPLE SERVICE 00100000006kRmI]|Account name to upper case|01Q000000009Gei|ON_ALL_CHANGES
08:57:36.569 (569098000)|WF_FORMULA|Formula:LEN(TRIM(Name))>0|Values:Name=APPLE SERVICE
08:57:36.569 (569111000)|WF_CRITERIA_END|true
08:57:36.569 (569131000)|WF_CRITERIA_BEGIN|[Account: APPLE SERVICE 00100000006kRmI]|Update date for monthly report|01Q000000009Q2t|ON_ALL_CHANGES
08:57:36.569 (569323000)|WF_FORMULA|Formula:(!ISBLANK( OSTI_Significant_Event__c )) && (!ISNULL( OSTI_Significant_Event__c ))|Values:OSTI_Significant_Event__c=null
08:57:36.569 (569335000)|WF_CRITERIA_END|false
08:57:36.569 (569351000)|WF_CRITERIA_BEGIN|[Account: APPLE SERVICE 00100000006kRmI]|Notify Admin on new Account|01Q000000009WZA|ON_CREATE_ONLY
08:57:36.569 (569357000)|WF_RULE_NOT_EVALUATED
08:57:36.569 (569368000)|WF_CRITERIA_BEGIN|[Account: APPLE SERVICE 00100000006kRmI]|Update AT Acct Mgr from hidden|01Q000000009Ved|ON_ALL_CHANGES
08:57:36.569 (569419000)|WF_RULE_FILTER|[Account : Hidden_AT_Acct_Mgr not equal to null]
AND [Account : Billing Country equals USA, CANADA, CAN, US]
08:57:36.569 (569828000)|WF_RULE_EVAL_VALUE|Rob Sepa
08:57:36.569 (569842000)|WF_RULE_EVAL_VALUE|CANADA
08:57:36.569 (569851000)|WF_CRITERIA_END|true
08:57:36.569 (569874000)|WF_SPOOL_ACTION_BEGIN|Workflow
08:57:36.579 (579103000)|WF_ACTION| Field Update: 7;
08:57:36.579 (579112000)|WF_RULE_EVAL_END
08:57:36.579 (579178000)|WF_ACTIONS_END| Field Update: 7;
08:57:36.579 (579189000)|CODE_UNIT_FINISHED|Workflow:Account
You could try below:
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);
}
// could not find delimiter, then try space instead for Canada zip code
else
{
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;
}
}
}
}
}
Coco
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);
}
// could not find delimiter, then try space instead for Canada zip code
else
{
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;
}
}
}
}
}
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);
}
// could not find delimiter, then try space instead for Canada zip code
else
{
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);
}
// could not find delimiter, then try space instead for Canada zip code
else
{
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;
}
}
}
}
}
*** Deployment Log ***
Result: FAILED
Date: March 11, 2014 9:27:56 AM PDT
# Deployed From:
Project name: move trigger to production
Username: richard_harris@sti.com.omron1
Endpoint: test.salesforce.com
# Deployed To:
Username: richard_harris@sti.com
Endpoint: www.salesforce.com
# Deploy Results:
File Name: package.xml
Full Name: package.xml
Action: UPDATED
Result: SUCCESS
Problem: n/a
File Name: triggers/UpdateZipCode.trigger
Full Name: UpdateZipCode
Action: UPDATED
Result: SUCCESS
Problem: n/a
# Test Results:
Run Failures:
TestUpdateZipCode.myUnitTest System.QueryException: List has no rows for assignment to SObject
UpdateZipCode Test coverage of selected Apex Trigger is 0%, at least 1% test coverage is required
Average test coverage across all Apex Classes and Triggers is 64%, at least 75% test coverage is required.
This is the test class for the trigger, please paste the code here let me have a look.
The first error "System.QueryException: List has no rows for assignment to SObject", you are getting is due to the fact that the query doesn't return any rows.
So if you check your test class "TestUpdateZipCode", you will see a SOQL quert at line 27 and when you run this query in developer console, you will see that the query is not returning any rows.
So here you need to make sure that your query returns a value instead of null, to get rid of this error. And moreover this is happening not only in production but in sandbox as well if you do a run test of this class, you will be able to check it.
Also you need to make sure that to deploy code from sandbox to production, your code coverage should be atleast above 75% and so you would have to increase the code coverage as well for the trigger you are deploying. Once you do that, you will not get code coverage error. Best practice aim at achieving 100% code coverage where ever you can.