You need to sign in to do that
Don't have an account?

codecoverage on trigger iam getting only 66% can any one helpme
i wrote this code;
trigger contactstrigger on Contact (after insert,after delete) {
if(Trigger.isInsert && Trigger.isAfter)
TriggerContact.increment(Trigger.New);
else
TriggerContact.increment(Trigger.Old);
public class TriggerContact {
public static void increment(List<Contact> cons){
set<Id> accid=new Set<id>();
for(Contact c:cons){
accId.add(c.AccountId);
}
List<Account> accs=[select id,count_of_contact__c ,(select id from Contacts ) from Account where id in:accId];
for(Account a:accs){
a.count_of_contact__c=a.contacts.size();
}
update accs;
}
}
testclass for this code
istest
public class testforcontact {
@istest
static void testme(){
account a1=new account(name='aaa',phone='9908294936');
insert a1;
contact c1=new contact(lastname='suresh', firstname='malli',accountid=a1.id);
insert c1;
list<account>acc=[select id,count_of_contact__c,(select id,lastname from contacts) from account where id=:c1.id];
for(account a:acc){
a.count_of_contact__c=a.contacts.size();
}
update acc;
}
}
trigger contactstrigger on Contact (after insert,after delete) {
if(Trigger.isInsert && Trigger.isAfter)
TriggerContact.increment(Trigger.New);
else
TriggerContact.increment(Trigger.Old);
public class TriggerContact {
public static void increment(List<Contact> cons){
set<Id> accid=new Set<id>();
for(Contact c:cons){
accId.add(c.AccountId);
}
List<Account> accs=[select id,count_of_contact__c ,(select id from Contacts ) from Account where id in:accId];
for(Account a:accs){
a.count_of_contact__c=a.contacts.size();
}
update accs;
}
}
testclass for this code
istest
public class testforcontact {
@istest
static void testme(){
account a1=new account(name='aaa',phone='9908294936');
insert a1;
contact c1=new contact(lastname='suresh', firstname='malli',accountid=a1.id);
insert c1;
list<account>acc=[select id,count_of_contact__c,(select id,lastname from contacts) from account where id=:c1.id];
for(account a:acc){
a.count_of_contact__c=a.contacts.size();
}
update acc;
}
}
Below is the Test Class:
Please do let me know if it helps you.
Regards,
Mahesh
Below is the code which is used for real roll up summary calculation:
Please do let me know if it helps you.
Regards,
Mahesh
Please mark it as solved, so that it will be helpful for others to follow the solution.
Regards,
Mahesh