2

I need some help with logic. I have a trigger where users will input zip codes, and on these zip codes there is a few requirements such as length and value types. Now with certain countries they do not require a zip code, and this is where I need help. I don't want to break out of my trigger if the value is null, but I also want to check the inputted values just in case they do input a value. Any ideas?

else if ((optionalZipCode.contains(countryKey) || (s.Zip_Postal_Code__c == null))) { 
                                System.debug('ZipCode is null.');                                                      
            if ((countryKey == 'CH' || countryKey == 'NZ' || countryKey == 'TN' ||    countryKey == 'VE') && (s.Zip_Postal_Code__c.length() == 4)) { //Condition for length of  4!
                System.debug('Inside ' + countryKey + ' this zip code matches');
            }
            if ((countryKey == 'MX' || countryKey == 'TH' || countryKey == 'TR') && (s.Zip_Postal_Code__c.length() <= 5)) { //Condition for length of  5!
                System.debug('Inside ' + countryKey + ' this zip code matches');
            }
            if ((countryKey == 'RU' || countryKey == 'CO') && (s.Zip_Postal_Code__c.length() <= 6)) { //Condition for length of  6!
                System.debug('Inside ' + countryKey + ' this zip code matches');
            }
            if ((countryKey == 'AU' || countryKey == 'CL') && (s.Zip_Postal_Code__c.length() <= 7)) { //Condition for length of  7!
                System.debug('Inside ' + countryKey + ' this zip code matches');               
          } else {
            System.debug('The countryCode ' + countryKey + 's' + ' zipcode is not the proper length. There is an error inside the Zipcode Field');     
            break;       
         }

Also what would a test class look like? Ill show you what I have and tried.

s.Quantity_of_Samples__c = 2;
    s.Override__c = 'Yes';
    s.Country__c = 'CH';
    s.State_Province__c = 'Luxembourg';
    s.Zip_Postal_Code__c = '12345'; 
    //s.Zip_Postal_Code__c = ''; 
    //s.Zip_Postal_Code__c = null; 
4
  • 1
    What logic would you like to perform, it's not clear from your question.
    – greenstork
    Mar 5, 2014 at 0:28
  • I don't want to stop people from putting in values for optional countries zip codes, but I also want to validate whatever their input is. So I think this might come from my test class, right now if I put in S.Zip_Postal_Code__c = null; I get System.Asset Exception execution of BeforeInsert, because it is expecting a value to be passed. So how do I test something like that?
    – EricSSH
    Mar 5, 2014 at 0:34
  • Not to take this conversation off tangent, but this seems like a good case for a Validation rule. Why is apex needed here?
    – greenstork
    Mar 5, 2014 at 0:46
  • I have annoying restrictions inside my Sampling__c object that don't allow picklist values, along with over 1500 unique values to check. This is my very first trigger, so I might of been able to talk my way into something like that by my inexperience led me to this.
    – EricSSH
    Mar 5, 2014 at 0:54

2 Answers 2

3

Not sure if this will fit your needs or not since I could not tell from your formatting. But assuming the last else is inside the main else if block this should help...

else if ((optionalZipCode.contains(countryKey) || (s.Zip_Postal_Code__c == null))) { 
                                    System.debug('ZipCode is null.');                                                      
    if(!String.isBlank(s.Zip_Postal_Code__c)){            
             if ((countryKey == 'CH' || countryKey == 'NZ' || countryKey == 'TN' ||    countryKey == 'VE') && (s.Zip_Postal_Code__c.length() == 4)) { //Condition for length of  4!
                    System.debug('Inside ' + countryKey + ' this zip code matches');
                }
                if ((countryKey == 'MX' || countryKey == 'TH' || countryKey == 'TR') && (s.Zip_Postal_Code__c.length() <= 5)) { //Condition for length of  5!
                    System.debug('Inside ' + countryKey + ' this zip code matches');
                }
                if ((countryKey == 'RU' || countryKey == 'CO') && (s.Zip_Postal_Code__c.length() <= 6)) { //Condition for length of  6!
                    System.debug('Inside ' + countryKey + ' this zip code matches');
                }
                if ((countryKey == 'AU' || countryKey == 'CL') && (s.Zip_Postal_Code__c.length() <= 7)) { //Condition for length of  7!
                    System.debug('Inside ' + countryKey + ' this zip code matches');               
              } 

        else {
                System.debug('The countryCode ' + countryKey + 's' + ' zipcode is not the proper length. There is an error inside the Zipcode Field');     
                break;       
             }
}
1

I have annoying restrictions inside my Sampling__c object that don't allow picklist values, along with over 1500 unique values to check. This is my very first trigger, so I might of been able to talk my way into something like that by my inexperience led me to this.

This may also be the time to write your first class and perhaps to learn about pattern matching. With the many restrictions you've placed upon yourself, it sounds as though you've largely defeated the purpose of the State and Country Picklists. If you haven't already, I suggest you read an explore this link to determine if it's appropriate to implement them in your organization and make your case to management.

That having been said, because of the size of the database you're working with to compare cities, states and zips, you may want to write a class to offload the processing of your input data rather than keep it within your trigger. You could call the class with a single line, and return the results to your trigger for further processing; greatly simplifying your code.

I see an inherent problem with the line of code below. The second part (s.Zip_Postal_Code__c == null) may throw a null pointer exception whenever it equates to true.

else if ((optionalZipCode.contains(countryKey) || (s.Zip_Postal_Code__c == null))) { 
                                System.debug('ZipCode is null.');                

You'll want to replace that part with (s.Zip_Postal_Code__c.isEmpty() == true). Apex does not like accessing null objects. It handles "Empty" objects different, especially strings. ('' is the equivalent of null for a string. See String methods in the Apex Documentation for more info).

If you stick with your original code above, you'd want to surround it with a "try-catch" block that includes }catch(nullpointerexception e){ followed by some code telling Apex what to do in the event of a null pointer exception. Don't fret, this is your first trigger and these things are learned from experience.

That's going to be the issue in general you're going to face repeatedly because of all the instances you've mentioned where null values are permitted by your organization. You'll need to catch the nullpointerexceptions then tell your code how to handle each of those special cases (RegEx would likely be very helpful to you here for screening common instances).

Sound fun? Lets just say there was a reason why the pick lists were developed and why validation rules have been recommended instead of writing a trigger for this purpose.

I hope you find this reply helpful in understanding the task you're facing in implementing this trigger. Its not a trivial task.

1
  • Wow this is great, thanks every one for taking time to respond to me. This community sure is good to learn from..
    – EricSSH
    Mar 5, 2014 at 17:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .