You need to sign in to do that
Don't have an account?
Abhishek Sharma 527
Test Case for custom roll up summary
Hello There, I have custom roll up summary handler class and trigger created seperately, I'm not aware about testing and don't know whether it will be written for handler class or trigger program.
also can anyone help in adding test case for this insertMethod (I have 2 other method, for which I will implement with that reference )
//handler class
public class Contact_count_Handler
{
public static void InsertMethod(List<Contact> newList){
List<Account> accList = new List<Account>();
try{
Set<id> accountIds = new Set<id>();
if(newList!=null) {
for(contact c: newList)
{
if(c.accountId!=null)
{
accountIds.add(c.accountId);
}
}
if(!accountIds.isEmpty()){
for(Account acc :[Select id,Number_of_linked_contact__c,(Select id,name from contacts) from Account where Id in : accountIds]){
acc.Number_of_linked_contact__c= acc.contacts.size();
acclist.add(acc);
}
if(acclist.size()>0){
update accList;
}
}
}
}
catch(Exception e){
System.debug('insert method execution under catch block');
}
}
}
//trigger code
trigger Contact_count_trigger on Contact (after insert, after update, after delete) {
if(trigger.isAfter && Trigger.isInsert){
Contact_count_Handler.InsertMethod(trigger.new);
}
}
also can anyone help in adding test case for this insertMethod (I have 2 other method, for which I will implement with that reference )
//handler class
public class Contact_count_Handler
{
public static void InsertMethod(List<Contact> newList){
List<Account> accList = new List<Account>();
try{
Set<id> accountIds = new Set<id>();
if(newList!=null) {
for(contact c: newList)
{
if(c.accountId!=null)
{
accountIds.add(c.accountId);
}
}
if(!accountIds.isEmpty()){
for(Account acc :[Select id,Number_of_linked_contact__c,(Select id,name from contacts) from Account where Id in : accountIds]){
acc.Number_of_linked_contact__c= acc.contacts.size();
acclist.add(acc);
}
if(acclist.size()>0){
update accList;
}
}
}
}
catch(Exception e){
System.debug('insert method execution under catch block');
}
}
}
//trigger code
trigger Contact_count_trigger on Contact (after insert, after update, after delete) {
if(trigger.isAfter && Trigger.isInsert){
Contact_count_Handler.InsertMethod(trigger.new);
}
}
Find the below answers.
1. If you get too many soql rows then use the @StartTesrt & @Stoptest. otherwise no need.
for more infomation refer the below link.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_start_stop_test.htm
2. Triggers fires whenever DML occurs on object, If any trigger are there for the partcular object then it will fire and it will pass the data to the respective apex class /trigger usring the trigger context variables like trigger.New & trigger.Old,Trigger.Newmap & trigger.oldmap.
3. Contact_count_trigger_Test should cover the code coverage for trigger & apex class.
Refer the below trailhead will help you, how write a test class.
https://trailhead.salesforce.com/content/learn/modules/apex_testing/apex_testing_triggers
If the above information helps, Please mark it as best answer.
Thanks!!
All Answers
try with below code.
If this helps, Please mark it as best answer.
Thanks!!
Try Below Test Class Please Mark It As Best Answer If It Helps
Thank You!
I) I have to add @start and @finish before insert con, is it?
II) and I have doubt, how it is getting linked with that handler class. I mean how we are confirming that it is test class for that particular apex program. maybe it's dumb question, plz guide
III) and do I have to write test case for trigger also (for this one)
trigger code
trigger Contact_count_trigger on Contact (after insert, after update, after delete) {
if(trigger.isAfter && Trigger.isInsert){
Contact_count_Handler.InsertMethod(trigger.new);
}
}
Find the below answers.
1. If you get too many soql rows then use the @StartTesrt & @Stoptest. otherwise no need.
for more infomation refer the below link.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_testing_tools_start_stop_test.htm
2. Triggers fires whenever DML occurs on object, If any trigger are there for the partcular object then it will fire and it will pass the data to the respective apex class /trigger usring the trigger context variables like trigger.New & trigger.Old,Trigger.Newmap & trigger.oldmap.
3. Contact_count_trigger_Test should cover the code coverage for trigger & apex class.
Refer the below trailhead will help you, how write a test class.
https://trailhead.salesforce.com/content/learn/modules/apex_testing/apex_testing_triggers
If the above information helps, Please mark it as best answer.
Thanks!!