You need to sign in to do that
Don't have an account?
SF7
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); } } }
@isTest
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
);
Test.StartTest();
Insert tstAcct;
Test.StopTest();
System.AssertEquals('1','No_Of_Child_Accounts__C');
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
Thanks
Akhil.
Hi,
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