3

I'm trying to Auto populate Transportation_Zone__c depending on my Country__c. So if my Country__c (which is a text field) == "US", then the Transportation_Zone__c must be auto populated to "USA". So what I do is grab the Country__c information from Sampling__c and store in it a set. Then I do a query on that said Set. I think this should work, but I'm not seeing why this isn't working. Given below is my Trigger and Test Class.. Thanks for you help.

Also this line isn't getting coverage.. Am I doing my test class wrong?

validCSet.add(s.Country__c);

Trigger

trigger updateSamplingTransZone on Sampling__c (before insert)
{
   Set < String > validCSet = new set < String > ();
system.debug('This is validCSet'  +validCSet);

 if (Trigger.IsInsert)
 {
  for (Sampling__c s: Trigger.new)  
  {
     if (s.Country__c != null)
        validCSet.add(s.Country__c); 
          system.debug('This is Country__c'  +s.Country__c);
  }      
  system.debug('This is ValidCSet  '  +validCSet+  '\n'); 
  Map < String, Validation_Region__c > validationRegionMap = new Map < String, Validation_Region__c > ([Select Id, Transportation_Zone__c, Country_Key__c from Validation_Region__c where Country_Key__c IN: validCSet]);
  for (Sampling__c s: Trigger.new)
  {
     if (s.Override__c == 'Yes')
     {
        s.SAP_Transportation_Zone__c = validationRegionMap.get(s.Country__c).Transportation_Zone__c;
     }
  }
}
}

Test Class

@isTest(SeeAllData=true)
 private class TestUpdateSamplingTransZone {

 static testMethod void UpdateSAPTransZone(){

  Validation_Region__c vr = new Validation_Region__c();
  vr.Name='TestProduct';   
  vr.Transportation_Zone__c='TestCountry';
  vr.Country_Key__c='TestText';

  insert vr;

  Sampling__c s = new Sampling__c();
   s.Name='TestEVB';
  s.Country__c = vr.Country_Key__c;
  s.Quantity_of_Samples__c =12;
  //s.Responsible_for_EVB_Build1__c=prod2.AE2__c;
  insert s;


  }

  static testMethod void UpdateSAPTransZone2(){
   Validation_Region__c vr2 = new Validation_Region__c();
   vr2.Name='TestProduct';   
  vr2.Transportation_Zone__c='TestCountry';
  vr2.Country_Key__c='TestTex2t';
  insert vr2;

 Sampling__c s2 = new Sampling__c();
  s2.Name='TestEVB';
  s2.Country__c = vr2.Country_Key__c;
  s2.Quantity_of_Samples__c =12;
  //s.Responsible_for_EVB_Build1__c=prod2.AE2__c;
  insert s2;

  }
}

1 Answer 1

5

Your map lookup is failing because populating a map from a SOQL select uses the record IDs as keys (see Maps of sObjects). You'll need to create your own map to do this:

trigger updateSamplingTransZone on Sampling__c (before insert)
{
    Set < String > validCSet = new set < String > ();
    system.debug('This is validCSet'  +validCSet);

    if (Trigger.IsInsert)
    {
        for (Sampling__c s: Trigger.new)  
        {
            if (s.Country__c != null)
                validCSet.add(s.Country__c); 
            system.debug('This is Country__c'  +s.Country__c);
        }      
        system.debug('This is ValidCSet  '  +validCSet+  '\n'); 

        // Get validation regions into a list...
        List < Validation_Region__c > validationRegionList =
            [Select Id, Transportation_Zone__c, Country_Key__c 
             from Validation_Region__c 
             where Country_Key__c IN: validCSet];

        // and make a map
        Map < String, Validation_Region__c > validationRegionMap = new Map < String, Validation_Region__c >();
        for (Validation_Region__c vr : validationRegionList) {
            validationRegionMap.put(vr.Country_Key__c, vr);
        }
        for (Sampling__c s: Trigger.new)
        {
            if (s.Override__c == 'Yes')
            {
                s.SAP_Transportation_Zone__c = validationRegionMap.get(s.Country__c).Transportation_Zone__c;
            }
        }
    }
}

Not sure why your line of code is not being covered, but you should assert correct operation of the trigger, like this:

@isTest
private class TestUpdateSamplingTransZone {

    static testMethod void UpdateSAPTransZone(){

        Validation_Region__c vr = new Validation_Region__c();
        vr.Name='TestProduct';   
        vr.Transportation_Zone__c='TestCountry';
        vr.Country_Key__c='TestText';

        insert vr;

        Sampling__c s = new Sampling__c();
        s.Name='TestEVB';
        s.Country__c = vr.Country_Key__c;
        s.Quantity_of_Samples__c =12;
        //s.Responsible_for_EVB_Build1__c=prod2.AE2__c;
        // From your trigger, it looks like you need this line
        s.Override__c = 'Yes';
        insert s;

        // Check that country has been overridden
        System.assertEquals(vr.Transportation_Zone__c, s.Country__c);
    }

    static testMethod void UpdateSAPTransZone2(){
        Validation_Region__c vr2 = new Validation_Region__c();
        vr2.Name='TestProduct';   
        vr2.Transportation_Zone__c='TestCountry';
        vr2.Country_Key__c='TestTex2t';
        insert vr2;

        Sampling__c s2 = new Sampling__c();
        s2.Name='TestEVB';
        s2.Country__c = vr2.Country_Key__c;
        s2.Quantity_of_Samples__c =12;
        //s.Responsible_for_EVB_Build1__c=prod2.AE2__c;
        // This time test without the override
        insert s2;

        // Check that country has not been overridden
        System.assertEquals(vr2.Country_Key__c, s.Country__c);
    }
}
3
  • It looks like the updates you did to the trigger work well.. I just need to figure out why my Country__c isn't getting populated through my test class.. I believe that is that issue
    – EricSSH
    Apr 1, 2014 at 20:18
  • So I pushed it to my Dev1 and it works great. I passed my test class with a 77% but it didn't even go into stuff that matters. So my question is it okay to go forward knowing that my test class doesn't really work and I got 77% by sheer luck? Obviously the goal is 100% so I will continue to explorer the issue in my Test class. Thanks @Metadaddy
    – EricSSH
    Apr 1, 2014 at 20:35
  • Could you get a screenshot of the code coverage for the trigger and add that to the question? BTW - you shouldn't need to use SeeAllData=true, since you're creating test data. I just removed that from my answer.
    – metadaddy
    Apr 2, 2014 at 0:39

You must log in to answer this question.

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