3

Some quick background of what I'm trying to do..

The following is my Apex Trigger. It is a validation Trigger on Sampling_c which will validate that Country_c is a Valid country using Validation_country_c. When it validates Country_c it will look into State_province_c and will then validate that it is actually a state So .. If Country_c = US and State_province_c = CA then we are okay. But IF Country_c = US and State_province_c = ZZ221(whatever) it should fail. Now these condition checks only triggers when Override_c = 'Yes'.

I've also written a test class that is ignoring some lines, but I will work one issue at a time. Which I'll put in below this because its tiny.

@IsTest
public class OverrideTest{
static testmethod void addressOverride(){

    Sampling__c s = new Sampling__c();        
s.Quantity_of_Samples__c = 2;
s.Override__c = 'Yes';
s.Country__c = 'US';
s.State_Province__c = 'CA';
s.Zip_Postal_Code__c = '93003';
insert S;
}
}

The Error that I am getting is System.DmlException:Insert failed. First Exception on row 0; first error : cannot_Insert_update_activate_entity, OverrideTrigger: Execution of Before Insert

Can anyone give me some guidance?

trigger OverrideTrigger on Sampling__c (before insert,before update) {

// Top level map is keyed by Country. Inner Map is keyed by Region   
Map<String, Map<String, Validation_Region__c>> validRegions = new Map<String,    Map<String, Validation_Region__c>>();
// ...

For(Validation_Region__c objR : [Select Id,Country_Key__c,Description__c,Name FROM Validation_Region__c]){

String countryKey = objR.Country_Key__c;

Map<String, Validation_Region__c> regionMap = validRegions.get(countryKey);
// Maybe rework to use Map.containsKey rather than null check. Would be cleaner.

if(regionMap == null) {
    regionMap = new Map<String, Validation_Region__c>();
    validRegions.put(countryKey, regionMap);
}
string regionKey = objR.Description__c; 
regionMap.put( regionKey,objR);
}


For( Sampling__c s : Trigger.new){
If((s.Country__c != null) && (S.Override__c == 'Yes')){
    String countryKey = s.Country__c;
    String regionKey = s.State_Province__c;

   // If(validCountries.containsKey(countryKey) && validRegions.containsKey(countryKey))   {
        // The country appears to be valid and there are possible Region matches
      //  Validation_Country__c vc = validCountries.get(countryKey);

        If(validRegions.get(countryKey).containsKey(regionKey)) {
            // The Region belongs to the country
            Validation_Region__c vr = validRegions.get(countryKey).get(regionKey);
            }

        }
    }

}

4
  • Does your test class fail if you comment out the entire trigger? I can't see anything in your trigger that would cause that error message. Is there some validation rule on Sampling__c?
    – willard
    Feb 26, 2014 at 22:55
  • My Test class doesn't fail, but it does skip a lot of columns 45% coverage. There are validations though, so I will deactivate them and see what happens. Thanks
    – EricSSH
    Feb 27, 2014 at 0:37
  • I removed all the validations on Sampling__c and it still gives me that error. I'm using eclipse to run the test script it appears it breaks at Insert S; which is why I believe it is in my trigger.
    – EricSSH
    Feb 27, 2014 at 0:46
  • Try a try {insert S; } catch(DmlException e) {system.assert(false,e.getMessage()+' '+ e.getLineNumber());} to get more info.
    – cropredy
    Feb 27, 2014 at 1:23

1 Answer 1

3

Your code should check if the country exists as a map first, or you'll get an error. Coincidentally, you're not using SeeAllData, and you're not entering any validation entries, so it would always yield an error.

// Bad
If(validRegions.get(countryKey).containsKey(regionKey)) {

// Good
if(validregions.get(countrykey) != null && validregions.get(countrykey).get(regionkey) != null) {

Also, you should consider gathering all country and region values in your trigger and filtering the first query by those values to avoid querying too much data.

Finally, make sure you insert some sample regions to validate with.

1
  • Thank you Fox that did the trick.. Now on to why my columns are being ignored.
    – EricSSH
    Feb 27, 2014 at 17:19

You must log in to answer this question.

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