2

I have a trigger that has two conditions, insert and update. On my update condition, I check to see if the Billing Zip Code has changed, if it has, the trigger fires.

My trigger works fine but I cannot cover the update condition with my test class for some reason. I can cover the update line but nothing after. It looks like the test class isn't getting the trigger to fire when I update the Billing Postal code in my test class.

Trigger:

trigger updateTerritoryonAccount on Account (before insert) {

    Map<String, String> map_ZipCode_TerrID = new Map<String, String>();
    Map<Id,Id> taMap = new Map<Id,Id>();
    for(Territory__c tr: [Select id,Zip_Codes__c, name from Territory__c where Zip_Codes__c != null AND Active__c != False]){
        for(String s : tr.Zip_Codes__c.split(',',0)){
            map_ZipCode_TerrID.put(s.trim(), tr.id);
        }
    }

    for(Territory_Assignment__c ta : [SELECT Id, User__c, Territory__c from Territory_Assignment__c WHERE Territory__c IN :map_ZipCode_TerrID.values()]){
        taMap.put(ta.User__c,ta.Territory__c);
    }

    if(Trigger.isInsert){
    for(Account a : Trigger.new){
        a.Territory__c = null;
        if(a.BillingPostalCode != null && a.BillingPostalCode !='' && a.BillingPostalCode.length() >= 5){
            String PostalCode = a.BillingPostalCode.subString(0,5);
            if(map_ZipCode_TerrID.get(PostalCode) != null && map_ZipCode_TerrID.get(PostalCode) == taMap.get(UserInfo.getUserId())){
        system.debug('--------------map_ZipCode_TerrID.get(PostalCode)---------------'+map_ZipCode_TerrID.get(PostalCode));
                a.Territory__c = map_ZipCode_TerrID.get(PostalCode); 
            }
            else{
                a.AddError('Territory error. Either you do not have access to this Territory or a Territory does not exist that contains the zip code for this Account. Please see your Administrator');
            }
        }
      }
    }

    if(Trigger.isUpdate ){
    for(Account a : Trigger.new){
        if(trigger.newMap.get(a.id).BillingPostalCode != trigger.oldMap.get(a.id).BillingPostalCode){
        a.Territory__c = null;
        if(a.BillingPostalCode != null && a.BillingPostalCode !='' && a.BillingPostalCode.length() >= 5){
            String PostalCode = a.BillingPostalCode.subString(0,5);
            if(map_ZipCode_TerrID.get(PostalCode) != null && map_ZipCode_TerrID.get(PostalCode) == taMap.get(UserInfo.getUserId())){
                a.Territory__c = map_ZipCode_TerrID.get(PostalCode); 
            }
            else{
                a.AddError('Territory error. Either you do not have access to this Territory or a Territory does not exist that contains the zip code for this Account. Please see your Administrator');
            }
          }
        }
      }
    }
}

Test class:

@isTest
    public class Test_updateTerritoryonAccount {

        private static testmethod void updateTerritoryonAccount_testMethod(){

            Profile p = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];

            User u = new User();
            u.FirstName = 'Test';
            u.LastName  = 'User12';
            u.Email     = 'testsuser@tests.com';
            u.Username  = 'testsuser@tests.com';
            u.Alias     = 'test23';
            u.ProfileId = p.id;
            u.TimeZoneSidKey    = 'America/Denver';
            u.LocaleSidKey      = 'en_US';
            u.EmailEncodingKey  = 'UTF-8';
            u.LanguageLocaleKey = 'en_US';
            insert u;

            Territory__c tr = new Territory__c ();
            tr.name = 'Test Tr';
            tr.Zip_Codes__c = '12345,14567,78741';
            insert tr;

            Territory_Assignment__c ta = new Territory_Assignment__c();
            ta.User__c = u.id;
            ta.Territory__c = tr.id;
            insert ta;

            system.runAs(u){
            test.startTest();

                    Account a2 = new Account();
                    a2.Name = 'Test Company';
                    a2.BillingPostalCode = '12345';
                    a2.BillingState = 'Texas';
                    a2.BillingStateCode = 'TX';
                    insert a2;

            test.stopTest();
            }
            try{
                    Account a3 = new Account();
                    a3.Name = 'Test Company';
                    a3.BillingPostalCode = '12345';
                    a3.BillingState = 'Texas';
                    a3.BillingStateCode = 'TX';
                    insert a3;

            }
            catch(Exception e){

            }
        }

        private static testmethod void updateTerritoryonAccount_testMethod2(){

                Profile p = [SELECT Id FROM Profile WHERE Name = 'System Administrator'];

            User u = new User();
            u.FirstName = 'Test';
            u.LastName  = 'User12';
            u.Email     = 'testsuser@tests.com';
            u.Username  = 'testsuser@tests.com';
            u.Alias     = 'test23';
            u.ProfileId = p.id;
            u.TimeZoneSidKey    = 'America/Denver';
            u.LocaleSidKey      = 'en_US';
            u.EmailEncodingKey  = 'UTF-8';
            u.LanguageLocaleKey = 'en_US';
            insert u;

            Territory__c tr = new Territory__c ();
            tr.name = 'Test Tr';
            tr.Zip_Codes__c = '12345,14567,78741';
            insert tr;

            Territory_Assignment__c ta = new Territory_Assignment__c();
            ta.User__c = u.id;
            ta.Territory__c = tr.id;
            insert ta;

            Account a = new Account();
            a.Name = 'Testing ACcount1';
            insert a;

            system.runAs(u){
                a.BillingPostalCode = '12345';
                update a;
            }
        }
    }

Part of test class that isn't covered in update condition:

for(Account a : Trigger.new){
        if(trigger.newMap.get(a.id).BillingPostalCode != trigger.oldMap.get(a.id).BillingPostalCode){
        a.Territory__c = null;
        if(a.BillingPostalCode != null && a.BillingPostalCode !='' && a.BillingPostalCode.length() >= 5){
            String PostalCode = a.BillingPostalCode.subString(0,5);
            if(map_ZipCode_TerrID.get(PostalCode) != null && map_ZipCode_TerrID.get(PostalCode) == taMap.get(UserInfo.getUserId())){
                a.Territory__c = map_ZipCode_TerrID.get(PostalCode); 
            }
            else{
                a.AddError('Territory error. Either you do not have access to this Territory or a Territory does not exist that contains the zip code for this Account. Please see your Administrator');
            }
4

Your Trigger is handling only before insert event. you need to add the update event (before / after) that you want to handle in the trigger definition.

trigger updateTerritoryonAccount on Account (before insert, before update) {

also, couple of suggestions

  • the test class has lot of repeated code.. you should consider moving the common code into separate helper methods and use them in the main test methods.
  • you can also consider using @TestSetup methods to create the user, profile, account you need and then query and use them in your test methods
  • Wow, I am a moron. Cannot believe I didn't catch that. Thanks @vamsi – sfdcJameson Jun 14 '15 at 17:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.