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
Justin.WilliamsJustin.Williams 

Test class not convering insert but not update of the test record.

I have been trying to write test code recently that seems to not work like it used to.  In the test class I would create a record and insert it, then make a change and update it.  That used to work fine now it is as if only the insert is recognized and the code related to the update doesn't get checked.

<pre>
public class AccountTeamManage Implements Dispatch.ITriggerEntry{
   
    public static list<AccountTeamMember> MemberList = null;
    public static list<AccountShare> RulesList = null;
    public static list<AccountTeamMember> RemovalList = null;
    public static map<Id,AccountTeamMember> CurrentTeam = null;
   
    Public Static Void MainEntry(Boolean IsUpdate, Boolean IsInsert, Boolean IsDelete, Boolean IsExecuting, Boolean IsBefore, Boolean IsAfter, LIST<sObject> NewList, Map<Id,sObject> NewMap, LIST<sObject> OldList, MAP<Id,sObject> OldMap){
        Map<Id,Account>NewAccount = (Map<Id,Account>)NewMap;
        Map<Id,Account>OldAccount = (Map<Id,Account>)OldMap;       
        MemberList = new List<AccountTeamMember>();
        RulesList = new List<AccountShare>();   
        RemovalList = new list<AccountTeamMember>();       
        CurrentTeam = new map<Id,AccountTeamMember>();
       
        for(AccountTeamMember Team : [SELECT UserId, AccountID, TeamMemberRole FROM AccountTeamMember WHERE AccountId IN:NewAccount.keySet()]){
            CurrentTeam.put(Team.UserId, Team);
        }
       
        for(Account a : NewAccount.values()){
            if(IsInsert){               
                if(a.CPMb_Sales__c != null){CreateTeam('CPMB Sales Consultant', a.Id, a.CPMb_Sales__c, a.OwnerId);}
                if(a.CPMb_Support__c != null){CreateTeam('CPMB Solution Consultant', a.Id, a.CPMb_Support__c, a.OwnerId);}
                if(a.SubEx_Sales__c != null){CreateTeam('Submittal Exchange Sales Consultant', a.Id, a.SubEx_Sales__c, a.OwnerId);}
                if(a.SubEx_NBR__c != null){CreateTeam('Submittal Exchange NBR', a.Id, a.SubEx_NBR__c, a.OwnerId);}
                if(a.SubEx_Support__c != null){CreateTeam('Submittal Exchange CRC', a.Id, a.SubEx_Support__c, a.OwnerId);}               
                if(a.Gradebeam_Sales__c != null){CreateTeam('Gradebeam Sales Consultant', a.Id, a.Gradebeam_Sales__c, a.OwnerId);}
                if(a.PQM_Sales__c != null){CreateTeam('PQM Sales Consultant', a.Id, a.PQM_Sales__c, a.OwnerId);}
                if(a.BidOrganizer_Sales__c != null){CreateTeam('BidOrganizer Sales Consultant', a.Id, a.BidOrganizer_Sales__c, a.OwnerId);}
                if(a.PlanSwift_Sales__c != null){CreateTeam('PlanSwift Sales Consultant', a.Id, a.PlanSwift_Sales__c, a.OwnerId);}               
            }else if(IsUpdate){
                Account oldAcct = OldAccount.get(a.Id);               
                UpdateTeam('CPMB Sales Consultant', a.Id, OldAcct.CPMb_Sales__c, a.CPMb_Sales__c, OldAcct.OwnerId, a.OwnerId);
                UpdateTeam('CPMB Solution Consultant', a.Id, OldAcct.CPMb_Support__c, a.CPMb_Support__c, OldAcct.OwnerId, a.OwnerId);               
                UpdateTeam('Submittal Exchange Sales Consultant', a.Id, OldAcct.SubEx_Sales__c, a.SubEx_Sales__c, OldAcct.OwnerId, a.OwnerId);               
                UpdateTeam('Submittal Exchange NBR', a.Id, OldAcct.SubEx_NBR__c, a.SubEx_NBR__c, OldAcct.OwnerId, a.OwnerId);               
                UpdateTeam('Submittal Exchange CRC', a.Id, OldAcct.SubEx_Support__c, a.SubEx_Support__c, OldAcct.OwnerId, a.OwnerId);               
                UpdateTeam('Gradebeam Sales Consultant', a.Id, OldAcct.Gradebeam_Sales__c, a.Gradebeam_Sales__c, OldAcct.OwnerId, a.OwnerId);               
                UpdateTeam('PQM Sales Consultant', a.Id, OldAcct.PQM_Sales__c, a.PQM_Sales__c, OldAcct.OwnerId, a.OwnerId);
                UpdateTeam('BidOrganizer Sales Consultant', a.Id, OldAcct.BidOrganizer_Sales__c, a.BidOrganizer_Sales__c, OldAcct.OwnerId, a.OwnerId);
                UpdateTeam('PlanSwift Sales Consultant', a.Id, OldAcct.PlanSwift_Sales__c, a.PlanSwift_Sales__c, OldAcct.OwnerId, a.OwnerId);
            }
        }
       
        if(RemovalList.size() > 0){
            delete RemovalList;
        }       
        if(MemberList.size() > 0){
            upsert MemberList;
        }       
        if(RulesList.size() > 0){
         upsert RulesList;
        }
    }
   
    private static void CreateTeam(string TeamName, Id AccId,Id NewMember,Id NewOwner){
        accountTeamMember ATeam = new AccountTeamMember(AccountId = AccId,TeamMemberRole = TeamName,UserId =NewMember);
        MemberList.add(ATeam);
        if(NewMember != NewOwner){
            AccountShare ATeamRules = new AccountShare(AccountId=AccId,OpportunityAccessLevel='Read',CaseAccessLevel='Read',AccountAccessLevel='Edit',UserOrGroupId=NewMember);
            RulesList.add(ATeamRules);
        }
    }
   
    private static void UpdateTeam(string TeamName, Id AccId, Id OldMember, Id NewMember, Id OldOwner, Id NewOwner){
        if(NewMember == null && OldMember != null){
            RemoveTeam(OldMember, TeamName);
        }
       
        if(NewMember != null){
            if(NewMember != OldMember){
                RemoveTeam(OldMember, TeamName);
            }
           
            if(!CurrentTeam.containsKey(NewMember)){
                CreateTeam(TeamName, AccId, NewMember, NewOwner);
            }else{
                if(CurrentTeam.get(NewMember).TeamMemberRole != TeamName){
                    CreateTeam(TeamName, AccId, NewMember, NewOwner);
                }else if(OldOwner != NewOwner && NewMember == OldOwner){
                    AccountShare ATeamRules = new AccountShare(AccountId=AccId,OpportunityAccessLevel='Read',CaseAccessLevel='Read',AccountAccessLevel='Edit',UserOrGroupId=NewMember);
              RulesList.add(ATeamRules);                   
                }
            }
        }
    }
   
    private static void RemoveTeam(Id OldMember, String TeamName){
        if(CurrentTeam.containsKey(OldMember)){
            if(CurrentTeam.get(OldMember).TeamMemberRole == TeamName){RemovalList.add(CurrentTeam.get(OldMember));}
        }
    }
   
    Public Static Void InProgressEntry(Boolean IsUpdate, Boolean IsInsert, Boolean IsDelete, Boolean IsExecuting, Boolean IsBefore, Boolean IsAfter, LIST<sObject> NewList, Map<Id,sObject> NewMap, LIST<sObject> OldList, MAP<Id,sObject> OldMap){
    }   
}
</pre>
Justin.WilliamsJustin.Williams
Here is my test code.  Its simple because I haven't spent the time to finish it out until I can figure this issue out.  This simple test so far gives me 50% covarage, basically everything in the Insert block. 

<pre>
@isTest
public class AccountTeamManageTest{
    static testMethod void BasicTestCase(){
        User u1 = [Select ID FROM User WHERE profile.name = 'System Administrator' AND isActive = true LIMIT 1];
        User u2 = [Select ID FROM User WHERE profile.name = 'Sales Team' AND isActive = true LIMIT 1];
        ID ARTID = Schema.SObjectType.Account.getRecordTypeInfosByName().get('Submittal Exchange').getRecordTypeId();
       
        System.RunAs(u1){
            test.startTest();
            Account a = new Account();
            a.Name = 'new account';
            a.OwnerId = u1.Id;
            a.Type = 'General Contractor';
            a.RecordTypeId = ARTID;
            a.CPMb_Sales__c = u1.Id;
            insert a;
           
            a.CPMb_Sales__c = u2.Id;
            test.stopTest();
        }
    }
}
</pre>