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
EricSSHEricSSH 

APEX Validation Trigger

Hello All,
I have been assigned a pretty daunting task to create a validation trigger for Ship to information.  I am very stressed out considering I have 0 experince inside the Salesforce enviroment.  Requirements below..

-------------
This support is needed on the Sampling__c object.
When saved with field Override__c = "Yes", we need to verify
1. that values entered in the Country__c field are are restricted to those listed on the attached file on the first tab in the Valid Country Codes column (column A)
2. that values in the State_Province__c field are restricted to those listed as valid for the entered Country__c value as noted on the second tab (column C lists valid options for the Country Code listed in column A)
3. that values entered in the Zip_Postal_Code__c field adhere to the attached rules for the entered Country__c as noted on the first tab (columns D, E and F indicate the rule for the Country Code listed in column A)
4. that values entered in the SAP_Transportation_Zone__c field are restricted to those listed as valid for the entered Country__c value as noted on the first tab (column c) – in fact if your trigger instead could auto-populate this field based on what is valid for the country code the user selects in the  Country__c field, that would be even better!

We are not able to accomplished this via dependent picklists as we feed these fields from our lead convert and custom address object and need to allow users to freely type values in those objects. We only want this validation to take place once the Sample record is saved with Override__c = "Yes" as stated above.----------

I will almost post the code that I have so far.   You guys have a load more of experince than I do so if you can think of a better way to do it please feel free to tell me..  Any insite is appericatied. 

trigger OverrideAddressTrigger on Sampling__c (before insert) {
     //String[] countryCodeZero = new String[]{'AD','AE','AF','AG','AI','AL','AM','AN','AO','AQ','AS','AW','AZ','BA','BB','BD','BF','BG','BH','BI','BJ','BM','BN','BO','BS','BT','BV','BW','BY','BZ','CC','CD','CF','CG','CI','CK','CM','CU','CV','CX','DJ','DM','DO','EC','EE','EG','EH','ER','ET','FJ','FK','FM','GA','GD','GE','GF','GH','GI','GL','GM','GN','GP','GQ','GS','GT','GU','GW','GY','HM','HN','HT','IO','IQ','JM','JO','KE','KG','KH','KI','KM','KN','KY','LA','LB','LC','LK','LR','LT','LY','MA','MD','MG','MH','MK','ML','MM','MN','MO','MP','MQ','MR','MS','MT','MU','MV','MW','MZ','NA','NC','NE','NF','NG','NI','NR','NU','OM','PA','PE','PF','PG','PK','PM','PN','PR','PW','PY','QA','RE','RW','SB','SC','SD','SH','SJ','SL','SM','SN','SO','SR','ST','SV','SY','SZ','TC','TF','TG','TJ','TK','TL','TM','TO','TP','TT','TV','TZ','UG','UM','UZ','VA','VC','VG','VI','VU','WF','WS','YE','YT','ZM','ZW'};
       
     Set<String> countryCodeZero = new Set<String>{'AD','AE','AF','AG','AI','AL','AM','AN','AO','AQ','AS','AW','AZ','BA','BB','BD','BF','BG','BH','BI','BJ','BM','BN','BO','BS','BT','BV','BW','BY','BZ','CC','CD','CF','CG','CI','CK','CM','CU','CV','CX','DJ','DM','DO','EC','EE','EG','EH','ER','ET','FJ','FK','FM','GA','GD','GE','GF','GH','GI','GL','GM','GN','GP','GQ','GS','GT','GU','GW','GY','HM','HN','HT','IO','IQ','JM','JO','KE','KG','KH','KI','KM','KN','KY','LA','LB','LC','LK','LR','LT','LY','MA','MD','MG','MH','MK','ML','MM','MN','MO','MP','MQ','MR','MS','MT','MU','MV','MW','MZ','NA','NC','NE','NF','NG','NI','NR','NU','OM','PA','PE','PF','PG','PK','PM','PN','PR','PW','PY','QA','RE','RW','SB','SC','SD','SH','SJ','SL','SM','SN','SO','SR','ST','SV','SY','SZ','TC','TF','TG','TJ','TK','TL','TM','TO','TP','TT','TV','TZ','UG','UM','UZ','VA','VC','VG','VI','VU','WF','WS','YE','YT','ZM','ZW'};
        System.debug('**********' +countryCodeZero);
        For(Sampling__c S: Trigger.new){
        //Creating the Rules for Zipcodes that have to be larger than 0
        IF((S.Override__c == 'Yes') && (S.Zip_Postal_Code__c.length() > 0)){  
           
            IF(string.ISBLANK(S.Country__c))
            {         
                System.debug('Country is Blank');
                //Do something
            }ELSE IF(string.isNotBlank(S.Country__c)){ 
     IF(countryCodeZero.contains(S.Country__c)
    {
                System.debug('Success!')
                return true;
             }ELSE{
                System.debug('Failure')
                return false;
           }
      }
               
            IF(string.ISBLANK(S.City__c))
            {
             System.debug('City is blank');
                //Do something
            }ELSE IF(string.isNotBlank(S.City__c)){
               //Do Stuff
            }
    
   IF(string.ISBLANK(S.State_Province__c))
            {
               System.debug('Province is blank');
                //Do something
            }ELSE IF(string.isNotBlank(S.State_Province__c)){
                //Do stuff
            }
            IF(string.ISBLANK(S.Zip_Postal_Code__c))
            {
             System.debug('Zip code is blank');
                //Do something
            }ElSE IF(S.Zip_Postal_Code__c != NULL){
                    System.debug('Zipcode filled');
            }ElSE{
              System.debug('Zipcode is Empty');
               
            }
                
            }
        }
    }
}
EricSSHEricSSH
Now that I have been thinking about this and getting deeper and deeper into the logic I am starting to realize that it isn't possible to do all of the validation with my current thinking.  The requirements need me to validate almost 2000 values so for instance ..

Country = US
City = CA/PA/NY and so on (And cannot equal any state/provinence outside its respective country)

I have values literally for every single country + all the states/provinces.  How can I manage this?  A Database?  I need guidance
EricSSHEricSSH
I created 2 objects now with all of the validation data .. Validation_Country__c and Validation_Region__c