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
Reshmi SmijuReshmi Smiju 

Account territorry Function Issue.

Hi,
I have an account.We will update its Billing Postal Code while insertion /updation of account. Each Billing postal code( Territory) is associated with the object called territory__c and its a master object for Territory_Member__c object(child), which contains users for that particular Territory.

Its trigger is working fine. In Its the test class, the Negative test part is not going through. When I am updating the bIlling Postal code with null value, the AssertEqauls method gives me error . Expected value is 0. actual value is 8 while querying the Account along with AccountShare object.. The trigger and test class as follows.

Trigger:
trigger TerritoryShare on Account (after insert, before update) {

    Set<String>zips = new Set<String>();
    if(Trigger.isInsert){
        for (Account a: Trigger.new){
            zips.add(a.BillingPostalCode);
        }
    }else if(Trigger.isUpdate){
        Set<Id>chngedAccs = new Set<Id>();
            for(Account a:Trigger.new){
                String oldzip = Trigger.oldMap.get(a.Id).BillingPostalCode;
                String newzip =a.BillingPostalCode;
                if(oldzip!= newzip){
                    zips.add(newzip);
                    chngedAccs.add(a.Id);
                }
            }    
        //delete old teritory
        List<AccountShare> shares =[SELECT Id FROM AccountShare WHERE Id IN:chngedAccs AND RowCause = 'Manual'];
        delete shares;
    }
        Map<String,Territory__c> termap = new Map<String,Territory__c>();
        List<Territory__c> terrs = [SELECT Id,Zip_Code__c,(SELECT Id,User__c FROM Territory_Members__r)
                                   FROM Territory__c WHERE Zip_Code__c IN:zips];
        for(Territory__c ter:terrs){
            termap.put(ter.Zip_Code__c,ter);
        }
        List<AccountShare> ashares = new List<AccountShare>();
        for(Account a:Trigger.new){
            Territory__c terr = termap.get(a.BillingPostalCode);
            if(terr!= null){
                for(Territory_Member__c tm : terr.Territory_Members__r){
                    if(tm.User__c !=a.OwnerId){
                       AccountShare ashare = new AccountShare();
                        ashare.AccountId = a.Id;
                        ashare.UserOrGroupId =tm.User__c;
                        ashare.AccountAccessLevel='Edit';
                        ashare.OpportunityAccessLevel ='Edit';
                        ashares.add(ashare);
                    }
              }
                
            }
        }
      
    insert ashares;
    }

TestClass

@isTest
public class TestTerritoryShare{
    static testMethod void  TestTerritorymethod(){
        //Insert 200 users
        List<User> users = new List<User>();
        for(Integer i=0;i<200;i++){
            User u= new User();
              u.FirstName         = 'piku';
              u.LastName          = 'thepanicker';
              u.Email             = 'piku@gmail.com';
              u.Alias             = 'mula' + i;
            u.Username          = 'mula' + i + '@gmail.com';
              u.LocaleSidKey      = 'en_US';
              u.TimeZoneSidKey    = 'GMT';
              u.ProfileID        = '00e90000001b52F';
              u.LanguageLocaleKey = 'en_US';
              u.EmailEncodingKey  = 'UTF-8';
              users.add(u);
        }
        insert users;
        List<Territory__c> terrs = new List<Territory__c>();
        for (Integer i=0; i<200;i++){
            Territory__c ter = new Territory__c();
            ter.Zip_Code__c = String.ValueOf(300 + i);
            terrs.add(ter);
        }
        insert terrs;
        
        //Insert terr Members
        List<Territory_Member__c> tms= new List<Territory_Member__c>();
        for(Integer i=0;i<200;i++) {
            Integer count = Integer.valueOf(Math.random()*20);
            for(Integer j=0; j<count;j++){
                Territory_Member__c tm = new Territory_Member__c();
                tm.Territory__c = terrs[i].Id;
                tm.User__c = users[j].Id;
                tms.add(tm);
            }
        }
        insert tms;
           Test.startTest();
        //insert accs
        List<Account>accs = new List<Account>();
        for(Integer i=0;i<200;i++){
            Account acc = new Account();
            acc.Name = 'Testvest';
            acc.BillingPostalCode =String.valueOf(300+i);
            accs.add(acc);
        }
        insert accs;
        //get accs with zipcode along with accountshare records
           accs=[SELECT Id,BillingPostalCode,(SELECT Id,AccountId,
                                           AccountAccessLEvel, OpportunityAccessLevel,UserorGroupId
                                           FROM Shares WHERE RowCause ='Manual') FROM Account];
        //Map to match zipcode with territories
        Map<String, Territory__c> terrmap = new Map<String,Territory__c>();
        terrs = [SELECT Id, Zip_Code__c,(SELECT Id,User__c FROM Territory_Members__r)FROM Territory__c];
        for(Territory__c ter:terrs){
            terrmap.put(ter.Zip_Code__c,ter);
        }
        //Assert each acc has ryt no of share records
        for(Account a:accs){
            Territory__c t= terrmap.get(a.BillingPostalCode);
            Integer ShareCount =a.shares.size();
            Integer membercount = t.Territory_Members__r.size();
            System.assertEquals(ShareCount,memberCount);
        }
            // Thing should not work
        List<Account> falsaccs = new List<Account>();
        for(Account a:accs){
            a.BillingPostalCode = null;
            falsaccs.add(a);
            }
        update falsaccs;
        // assert No account share
        falsaccs=[SELECT Id,BillingPostalCode,(SELECT Id,AccountId,
                                           AccountAccessLEvel, OpportunityAccessLevel,UserorGroupId
                                           FROM Shares WHERE RowCause ='Manual') FROM Account];
        for(Account a:falsaccs){
            Integer shareCount = a.Shares.size();
            System.assertEquals(0,shareCount); /// This gives me error.  Assertion Failed: Expected: 0, Actual: 8
            }
        //Update acc back with zipcode
        for(Integer i=0;i<200;i++){
            falsaccs[i].BillingPostalCode =String.valueOf(300+i);
            }
        update falsaccs;
        // assert again with ryt no share records.
        falsaccs =[SELECT Id,BillingPostalCode,(SELECT Id,AccountId,AccountAccessLevel,OpportunityAccessLevel,UserOrGroupId
                                            FROM Shares WHERE RowCause ='Manual')
                       FROM Account];
        For(Account a:falsaccs){
            Territory__c t= terrmap.get(a.BillingPostalCode);
             Integer ShareCount =a.shares.size();
            Integer membercount = t.Territory_Members__r.size();
            System.assertEquals(ShareCount,memberCount);
        }

            Test.stopTest();
     }    
  }

Please let me know, your valuable thought. Thanks in Advance.
Regards
Reshmi