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
Daniel MadhureDaniel Madhure 

Test class is Failing can any one tell how to fix this issue

Error ;- System.LimitException: Apex CPU time limit exceeded

Trigger.accountTrigger: line 35, column 1

trigger accountTrigger on Account (before update,before insert, before delete, After Insert, After Update, After Undelete) {
    
    boolean bByPass = false;
    if (UPSUtility.ByPassCurrentUser() != null && UPSUtility.ByPassCurrentUser().Bypass_Account_Object_Validation_Rules__c)
       bByPass = true;

    if (!bByPass){
        if(!Constants.bTriggerRecursionCheck)
        {
            if (Trigger.IsBefore && (Trigger.IsInsert || Trigger.IsUpdate)){
                Constants.bTriggerRecursionCheck = true;
                AccountTriggerHandler.validateProspectAccountForSite(Trigger.new);
                AccountTriggerHandler.UpdateDistrictName(trigger.new);
                 //AccountTriggerHandler.HIEChangeToNoneInsert(Trigger.new);
           }
            
            if (Trigger.IsBefore && Trigger.IsUpdate){
                AccountTriggerHandler.OwnerChangeUpdate(Trigger.oldMap,Trigger.newMap,Trigger.new); 
                //AccountTriggerHandler.HIEChangeToNoneUpdate(Trigger.oldMap,Trigger.newMap,Trigger.new);
                AccountTriggerHandler.assignedAccountRVARecordType(Trigger.old,Trigger.new,true); 
            }

            if (Trigger.IsBefore && Trigger.IsInsert){
                AccountTriggerHandler.assignedAccountRVARecordType(null,Trigger.new,false); 
            }
        }
        if(Trigger.isAfter && Trigger.isUpdate){
            AccountTriggerHandler.assignedLeadContactRVARecordType(Trigger.old,Trigger.new);
        }
    }
    
    if(Trigger.isAfter && (Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete)){
        Map<Id,Account> mapAccToProcess = new Map<Id,Account>();
         Set<Id>AccountIds = new set<ID>();
        for(Account acc : Trigger.New){
            if(acc.RecordType.DeveloperName != 'Site'){
                if(Trigger.isInsert || Trigger.isUndelete){
                    mapAccToProcess.put(acc.Id, acc);    
                }
                else if(acc.OwnerId != Trigger.oldMap.get(acc.Id).OwnerId || acc.Name != Trigger.oldMap.get(acc.Id).Name || acc.Account_Number_Interface_Key__c != Trigger.oldMap.get(acc.Id).Account_Number_Interface_Key__c){
                    mapAccToProcess.put(acc.Id, acc);
                    AccountIds.add(acc.Id);
                   system.debug('Account Ids' +AccountIds);
                    
                }
            }   
        }
        if(AccountIds.size() >0){
            AccountTriggerHandler.updatePTOwner(AccountIds);
        }
        if(mapAccToProcess.size() > 0){
            AccountTriggerHandler.createUpdateSAD(mapAccToProcess);
        }    
    }
    
    if(Trigger.isBefore && Trigger.isDelete){
        AccountTriggerHandler.deleteSAD(Trigger.oldMap);
    }    
    
    

}




















Test class-:
                
 
@isTest
private  class AccountHeirarchy_Build_Test 
{
   public static integer iter = 0;
   public static integer totalAccountCount = 0;
   
   static list<Account> insertAccountHeirarchy() 
    {
       list<Account> AllAccounts = new list<Account>();
       Account eGnAccount = new Account(name = 'EGN',Top_Level_Parent_Account_Number__c = 'ABC-123-DEF-456-GHIJ');   
       insert eGnAccount;    
       Account parnetAccount = eGnAccount;
       list<Account> SubLevelAccount = new list<Account>();
       
       for (integer iIterationCount =0;iIterationCount<10;iIterationCount++)
        {
            string AccName =  String.fromCharArray(new Integer[]{65 + iIterationCount}) + ':';
            SubLevelAccount = UtilityForTestClass.createAccountBulk( 100, parnetAccount,AccName);
            insert SubLevelAccount;
            system.debug('check ---'+SubLevelAccount.size());
            parnetAccount = SubLevelAccount[50];
            AllAccounts.addAll(SubLevelAccount);
        }   
       
       return AllAccounts;
    }
    
    static TestMethod void checkAccountHeirArchydesearilisation()
    {
        UPSTestUtility testUtilObj = new UPSTestUtility();
        
        Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator'];
        try
        {
            UPSD_Bypass_Validation_Rules_Triggers__c custSettingBypassValidation = testUtilObj.buildBypassValidationRulesCustomSettingRecord(p.id,'AccountHeirarchy_Build_Test');
            if(custSettingBypassValidation!=null)
            {
                insert custSettingBypassValidation;
            }
        }
        catch(Exception ex)
        {
            System.debug('Exception caught - '+ex.getMessage());
        }
        User stdUser = new User(Alias = 'standt', Email='standardUPSuser@UPSTEST.com',
            EmailEncodingKey='UTF-8', LastName='Testing', LanguageLocaleKey='en_US',
            TimeZoneSidKey='America/Los_Angeles',LocaleSidKey='en_US', ProfileId = p.Id,UserName='standardUPSuser@UPSTEST.com');
        insert stdUser;
        list<Account> AllAccounts = insertAccountHeirarchy();
        integer iRanDomAccountNumber = integer.Valueof(Math.Floor(Math.Random()*10));
        AccountTeamMember atm = new AccountTeamMember(userId = stdUser.id, AccountId = AllAccounts[iRanDomAccountNumber].id);
        insert atm;
        Account acct= new Account(name='test', Top_Level_Parent_Account_Number__c='ABCT123456609');
        insert acct;
        Account acct1= new Account(name='test', Top_Level_Parent_Account_Number__c='ABCT123456609', ParentId= acct.id);
        insert acct1;
        Account acct2= new Account(name='test', Top_Level_Parent_Account_Number__c='ABCT123456609', ParentId= acct1.id);
        insert acct2;
        
        integer iCount = [select count() from Account];
        system.debug('# Account Records--' + iCount);
        //AccountHeirarchy_Built AccHeir = new AccountHeirarchy_Built();
        Test.startTest();
        string jSonReturnVal = AccountHeirarchy_Built.GetAlltheAccountsOfUser(stdUser.id);
        //AccountHeirarchyPageClass.GetAlltheAccountsOfUser(acct1.Id);
        Test.stopTest();
        list<AccountHeirarchy_Built.AccountBox>  lstAccountBox = (list<AccountHeirarchy_Built.AccountBox>)JSON.deserialize(jSonReturnVal,list<AccountHeirarchy_Built.AccountBox>.class);
        integer iCountLstSize = lstAccountBox.size() - 1;
        //system.assert(lstAccountBox[0].sAccountname == 'EGN', 'Apex Account is not EGN');
        checkAssertion(lstAccountBox[0].lstchild );
        //system.assert(AllAccounts.size() == totalAccountCount, 'Account Insert do match with number of Account parsed');
        
    }
    
    static void checkAssertion(list<AccountHeirarchy_Built.AccountBox> lstAccountBox )
    {
        integer countThisIteration = 0;
        
        for (AccountHeirarchy_Built.AccountBox acc : lstAccountBox)
         {
            //system.assert(acc.sAccountName.contains(String.fromCharArray(new Integer[]{65 + iter}) + ':'),
                // '----sAccountName----' + acc.sAccountName + '---string ----' 
                //+ String.fromCharArray(new Integer[]{65 + iter}) + ':' + '---------pop--' + countThisIteration + '---Iter --- ' + iter);
                totalAccountCount++;
            if (!acc.lstchild.IsEmpty())
             {
                iter++;
                checkAssertion(acc.lstchild);
                iter--;
             }
            countThisIteration++;       
         }
    }
    
}