+ Start a Discussion

Need Help with Test class for Trigger

// The Purpose of this Trigger is to get the Count of Total Number of Childern the parent account has.

trigger countChildAccount on Account (after Insert, after Update, before delete) { Set<id> ids= new Set<id>(); List<Account> acclist = new List<Account>(); Integer count = 0; if(Trigger.isInsert || Trigger.isUpdate){ for(Account acc: Trigger.new){ if(acc.Ultimate_Parent_Client__c!=null) ids.add(acc.Ultimate_Parent_Client__c); acclist.add(acc); } } if(Trigger.isDelete){ for(Account acc: Trigger.old){ if(acc.Ultimate_Parent_Client__c!=null) ids.add(acc.Ultimate_Parent_Client__c); acclist.add(acc); } } if(ids.size()>0){ List<Account> accchild = new List<Account>([select id,Ultimate_Parent_Client__c from Account where Ultimate_Parent_Client__c IN: ids]); List<Account> accparent = new List<Account>([select id,No_of_Child_Accounts__c from Account where id IN: ids]); for(Account ac: accparent){ count =0; for(Account acchild: accchild){ if(acchild.Ultimate_Parent_Client__c == ac.id) count++; } ac.No_of_Child_Accounts__c = count; } try{ upsert accparent; }catch(DMLException ex){ System.debug('Exception is '+ex); } } }


private class TestCountChildAccount {

    static testMethod void AccountSumTest() {
        Account tstAcct = new Account ( Name = 'Test932',
                                       BillingCity ='ultAccforDSAutomationTestCity',
                                               BillingCountry ='ultAccforDSAutomationCountry',
                                               BillingStreet ='ultAccforDSAutomationSt',
                                               BillingPostalCode ='536768',
                                               phone = '010101',
                                               Ultimate_Parent__c = True
        insert tstAcct;

         Account tstAcct1 = new Account ( Name = 'Test932',
                                       BillingCity ='ultAccforDSAutomati1onTestCity',
                                               BillingCountry ='ultAccfo1rDSAutomationCountry',
                                               BillingStreet ='ultAccforD1SAutomationSt',
                                               BillingPostalCode ='5367618',
                                               phone = '0101101',
                                               Ultimate_Parent_Client__c = tstAcct.id
       Insert tstAcct;

        tstAcct.fax = '2344';
        update tstAcct;
        delete tstAcct;
        undelete tstAcct;




HI , I have a Trigger which counts the total number of child accounts the parent account has , I am having difficulty in getting test coverage , any help on this one Plz




Best Answer chosen by Admin (Salesforce Developers) 




You are not inserting tstAcct1 record that you've created after tstAcct and You need to query to get the updated value.


See the changes below

private class TestCountChildAccount {

    static testMethod void AccountSumTest() {
        Account tstAcct = new Account ( Name = 'Test932',
                                       BillingCity ='ultAccforDSAutomationTestCity',
                                               BillingCountry ='ultAccforDSAutomationCountry',
                                               BillingStreet ='ultAccforDSAutomationSt',
                                               BillingPostalCode ='536768',
                                               phone = '010101',
                                               Ultimate_Parent__c = True
        insert tstAcct;

         Account tstAcct1 = new Account ( Name = 'Test932',
                                       BillingCity ='ultAccforDSAutomati1onTestCity',
                                               BillingCountry ='ultAccfo1rDSAutomationCountry',
                                               BillingStreet ='ultAccforD1SAutomationSt',
                                               BillingPostalCode ='5367618',
                                               phone = '0101101',
                                               Ultimate_Parent_Client__c = tstAcct.id
       Insert tstAcct1;
       Account acc = [Select No_Of_Child_Accounts__c from Account where id = :tstAcct.id];
       tstAcct1.fax = '2344';
       update tstAcct1;
       delete tstAcct1;
       Account a = [Select No_Of_Child_Accounts__c from Acccount where id = :tstAcct.id];
System.AssertEqualts('0', a.No_Of_Child_Accounts__c); undelete tstAcct1; } }