function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
saintrjsaintrj 

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;
      }
      }
    }
    }
}
Coco_SdyneyCoco_Sdyney
Let me explain what I saw from this piece of code.

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.


saintrjsaintrj
Out Canadian zip codes are strictly Controles to the three space three format. And the zipcodetable name are three digit with no trailing characters, but only Us zip codes return a value
Coco_SdyneyCoco_Sdyney
um....then are you able to check the debug log?
when you insert/update a account having billingpostalcode with CAN zip code, could you paste the debug log here?
saintrjsaintrj
Is this what you need?
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
Coco_SdyneyCoco_Sdyney
Hi,


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
saintrjsaintrj
At save: Error: Compile Error: expecting a semi-colon, found '}' at line 20 column 60  Fixed. Then at next save attempt Error: Compile Error: Duplicate variable: i (attempt to re-create the variable with type: integer) at line 37 column 18  No clue,
Coco_SdyneyCoco_Sdyney
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;
      }
      }
    }
    }
}
saintrjsaintrj
I loaded it and created a new Canada account but no look-up so It appears still not functioning. After this runs I am looking for a number of fields to update with info from the zipcodetable object. Here is the debug log. 19.0 APEX_CODE,DEBUG;APEX_PROFILING,INFO;CALLOUT,INFO;DB,INFO;SYSTEM,DEBUG;VALIDATION,INFO;VISUALFORCE,INFO;WORKFLOW,INFO 15:22:57.310 (310823000)|ENTERING_MANAGED_PKG|SFSSDupeCatcher 15:22:57.375 (375163000)|SOQL_EXECUTE_BEGIN|[722]|Aggregations:0|select Name, CreatedDate, Id from Override_Cache__c where Session_Id__c = :tmpVar1 limit 50 15:22:57.380 (380664000)|SOQL_EXECUTE_END|[722]|Rows:0 15:22:57.382 (382766000)|SOQL_EXECUTE_BEGIN|[722]|Aggregations:0|select Name, CreatedDate, Id from Override_Cache__c where Session_Id__c = :tmpVar1 limit 50 15:22:57.384 (384905000)|SOQL_EXECUTE_END|[722]|Rows:0 15:22:57.405 (405885000)|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 15:22:57.415 (415912000)|SOQL_EXECUTE_END|[159]|Rows:0 15:22:57.426 (426520000)|CODE_UNIT_STARTED|[EXTERNAL]|01q000000008Tns|UpdateZipCode on Account trigger event BeforeUpdate for [001R000000orKAB] 15:22:57.426 (426838000)|SYSTEM_CONSTRUCTOR_ENTRY|[2]|() 15:22:57.426 (426874000)|SYSTEM_CONSTRUCTOR_EXIT|[2]|() 15:22:57.426 (426939000)|SYSTEM_METHOD_ENTRY|[6]|LIST.size() 15:22:57.426 (426969000)|SYSTEM_METHOD_EXIT|[6]|LIST.size() 15:22:57.427 (427083000)|SYSTEM_METHOD_ENTRY|[8]|System.debug(ANY) 15:22:57.427 (427102000)|USER_DEBUG|[8]|DEBUG|ZipCode: T2Z 2m5 15:22:57.427 (427108000)|SYSTEM_METHOD_EXIT|[8]|System.debug(ANY) 15:22:57.427 (427165000)|SYSTEM_METHOD_ENTRY|[23]|System.debug(ANY) 15:22:57.427 (427176000)|USER_DEBUG|[23]|DEBUG|ZipCode after the filter: T2Z 15:22:57.427 (427182000)|SYSTEM_METHOD_EXIT|[23]|System.debug(ANY) 15:22:57.427 (427206000)|SYSTEM_METHOD_ENTRY|[24]|LIST.add(Object) 15:22:57.427 (427225000)|SYSTEM_METHOD_EXIT|[24]|LIST.add(Object) 15:22:57.427 (427240000)|SYSTEM_METHOD_ENTRY|[6]|LIST.size() 15:22:57.427 (427246000)|SYSTEM_METHOD_EXIT|[6]|LIST.size() 15:22:57.427 (427263000)|SYSTEM_METHOD_ENTRY|[27]|LIST.size() 15:22:57.427 (427280000)|SYSTEM_METHOD_EXIT|[27]|LIST.size() 15:22:57.427 (427292000)|SYSTEM_METHOD_ENTRY|[27]|String.valueOf(Object) 15:22:57.427 (427301000)|SYSTEM_METHOD_EXIT|[27]|String.valueOf(Object) 15:22:57.427 (427309000)|SYSTEM_METHOD_ENTRY|[27]|System.debug(ANY) 15:22:57.427 (427314000)|USER_DEBUG|[27]|DEBUG|zipcode size: 1 15:22:57.427 (427318000)|SYSTEM_METHOD_EXIT|[27]|System.debug(ANY) 15:22:57.427 (427329000)|SYSTEM_METHOD_ENTRY|[29]|LIST.size() 15:22:57.427 (427344000)|SYSTEM_METHOD_EXIT|[29]|LIST.size() 15:22:57.473 (473952000)|SOQL_EXECUTE_BEGIN|[30]|Aggregations:0|select id, name from Zip_Code_Table__c where name IN :tmpVar1 15:22:57.478 (478778000)|SOQL_EXECUTE_END|[30]|Rows:1 15:22:57.479 (479004000)|SYSTEM_METHOD_ENTRY|[31]|LIST.size() 15:22:57.479 (479039000)|SYSTEM_METHOD_EXIT|[31]|LIST.size() 15:22:57.479 (479057000)|SYSTEM_METHOD_ENTRY|[31]|String.valueOf(Object) 15:22:57.479 (479067000)|SYSTEM_METHOD_EXIT|[31]|String.valueOf(Object) 15:22:57.479 (479078000)|SYSTEM_METHOD_ENTRY|[31]|System.debug(ANY) 15:22:57.479 (479085000)|USER_DEBUG|[31]|DEBUG|Data returned by zipcodeTable : 1 15:22:57.479 (479090000)|SYSTEM_METHOD_EXIT|[31]|System.debug(ANY) 15:22:57.479 (479098000)|SYSTEM_METHOD_ENTRY|[34]|LIST.size() 15:22:57.479 (479113000)|SYSTEM_METHOD_EXIT|[34]|LIST.size() 15:22:57.479 (479158000)|SYSTEM_METHOD_ENTRY|[37]|LIST.size() 15:22:57.479 (479175000)|SYSTEM_METHOD_EXIT|[37]|LIST.size() 15:22:57.479 (479259000)|SYSTEM_METHOD_ENTRY|[38]|MAP.put(Object, Object) 15:22:57.479 (479284000)|SYSTEM_METHOD_EXIT|[38]|MAP.put(Object, Object) 15:22:57.479 (479294000)|SYSTEM_METHOD_ENTRY|[37]|LIST.size() 15:22:57.479 (479300000)|SYSTEM_METHOD_EXIT|[37]|LIST.size() 15:22:57.479 (479324000)|SYSTEM_METHOD_ENTRY|[43]|LIST.size() 15:22:57.479 (479342000)|SYSTEM_METHOD_EXIT|[43]|LIST.size() 15:22:57.479 (479408000)|SYSTEM_METHOD_ENTRY|[52]|MAP.get(Object) 15:22:57.479 (479430000)|SYSTEM_METHOD_EXIT|[52]|MAP.get(Object) 15:22:57.479 (479443000)|SYSTEM_METHOD_ENTRY|[53]|String.valueOf(Object) 15:22:57.479 (479457000)|SYSTEM_METHOD_EXIT|[53]|String.valueOf(Object) 15:22:57.479 (479466000)|SYSTEM_METHOD_ENTRY|[53]|System.debug(ANY) 15:22:57.479 (479471000)|USER_DEBUG|[53]|DEBUG|zipcodeid: null 15:22:57.479 (479475000)|SYSTEM_METHOD_EXIT|[53]|System.debug(ANY) 15:22:57.479 (479571000)|SYSTEM_METHOD_ENTRY|[43]|LIST.size() 15:22:57.479 (479580000)|SYSTEM_METHOD_EXIT|[43]|LIST.size() 15:22:57.656 (479615000)|CUMULATIVE_LIMIT_USAGE 15:22:57.656|LIMIT_USAGE_FOR_NS|(default)| Number of SOQL queries: 1 out of 100 Number of query rows: 1 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 15:22:57.656|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 15:22:57.656|CUMULATIVE_LIMIT_USAGE_END 15:22:57.479 (479900000)|CODE_UNIT_FINISHED|UpdateZipCode on Account trigger event BeforeUpdate for [001R000000orKAB] 15:22:57.484 (484279000)|ENTERING_MANAGED_PKG|i 15:22:57.525 (525984000)|ENTERING_MANAGED_PKG|i 15:22:57.555 (555987000)|CODE_UNIT_STARTED|[EXTERNAL]|Validation:Account:001R000000orKAB 15:22:57.556 (556000000)|VALIDATION_RULE|03d000000004CJr|Canadian_Billing_Province_Validation 15:22:57.556 (556175000)|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 15:22:57.556 (556182000)|VALIDATION_PASS 15:22:57.556 (556185000)|VALIDATION_RULE|03d000000004FA4|OCI_Canadian_Postal_Code_Validation 15:22:57.556 (556313000)|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=T2Z 2m5 15:22:57.556 (556320000)|VALIDATION_PASS 15:22:57.556 (556322000)|VALIDATION_RULE|03d000000004PM1|Phone_Number_must_be_Numeric_Only 15:22:57.692 (692985000)|VALIDATION_FORMULA|And( $Profile.Name 0|Values:Name=APPLE SERVICE II 15:22:58.113 (1113486000)|WF_CRITERIA_END|true 15:22:58.133 (1133956000)|WF_CRITERIA_BEGIN|[Account: APPLE SERVICE II 001R000000orKAB]|Update date for monthly report|01Q000000009Q2t|ON_ALL_CHANGES 15:22:58.134 (1134319000)|WF_FORMULA|Formula:(!ISBLANK( OSTI_Significant_Event__c )) && (!ISNULL( OSTI_Significant_Event__c ))|Values:OSTI_Significant_Event__c=null 15:22:58.134 (1134336000)|WF_CRITERIA_END|false 15:22:58.134 (1134365000)|WF_CRITERIA_BEGIN|[Account: APPLE SERVICE II 001R000000orKAB]|Update AT Acct Mgr from hidden|01Q000000009Ved|ON_ALL_CHANGES 15:22:58.134 (1134450000)|WF_RULE_FILTER|[Account : Hidden_AT_Acct_Mgr not equal to null] AND [Account : Billing Country equals USA, CANADA, CAN, US] 15:22:58.135 (1135012000)|WF_RULE_EVAL_VALUE| 15:22:58.135 (1135031000)|WF_RULE_EVAL_VALUE|CANADA 15:22:58.135 (1135039000)|WF_CRITERIA_END|true 15:22:58.135 (1135068000)|WF_CRITERIA_BEGIN|[Account: APPLE SERVICE II 001R000000orKAB]|Notify Admin on new Account|01Q000000009WZA|ON_CREATE_ONLY 15:22:58.135 (1135075000)|WF_RULE_NOT_EVALUATED 15:22:58.135 (1135099000)|WF_SPOOL_ACTION_BEGIN|Workflow 15:22:58.137 (1137130000)|WF_ACTION| Field Update: 6; 15:22:58.137 (1137137000)|WF_RULE_EVAL_END 15:22:58.137 (1137208000)|WF_ACTIONS_END| Field Update: 6; 15:22:58.137 (1137214000)|CODE_UNIT_FINISHED|Workflow:Account Regards, Richard Harris Manager, Sales Information Systems Mobile 949-400-4199 Direct 949-380-4199 Fax 847 285-8915 richard_harris@sti.com Omron Automation and Safety One Commerce Drive, Schaumburg, IL, 60173 www.omron247.com www.sti.com This is a confidential message. If received in error, delete it and notify the sender.
Coco_SdyneyCoco_Sdyney
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);
        }
       // 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;
      }
      }
    }
    }
}
saintrjsaintrj
That seems to do it. Thank you so very much for your assistance.
saintrjsaintrj
OK, I attemopted to move it to the production environment using the Elicpse IDe, but I got a bumch of errors. Any ideas what is wrong log below
*** 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.
Coco_SdyneyCoco_Sdyney
ok, this class is poping up error :TestUpdateZipCode

This is the test class for the trigger, please paste the code here let me have a look.
saintrjsaintrj
What code do you want to see. Is there an error log some where, or the code imported...
saintrjsaintrj
Here is the response from salesforce.com support. Unfortunately I have no idea what this means

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.
Coco_SdyneyCoco_Sdyney
there is a apex class which name is 'TestUpdateZipCode', copy & paste its code here, let us to see what should do to modify it.