rollup summary with bulk record handling without hitting governer limits
Contact Trigger :
================================================
trigger ContactTrigger on Contact (before insert, after insert, before update, after update, before delete, after undelete, after delete) {
Set<Id> accIds = new Set<Id>();// set to store parent account ids of contacts
map<string,Account> accconmap1=new map<string,Account>();
map<string,string> accconmap=new map<string,string>();
map<string,string> accconmap2=new map<string,string>();
if(trigger.isAfter && (trigger.isInsert || trigger.isUndelete))
{
if(!trigger.new.isEmpty()){
Integer i=0;
for(Contact con : trigger.new)
{
accIds.add(con.AccountId);
accconmap.put(con.id,con.AccountId);
}
}
}
if(trigger.isAfter && trigger.isUpdate)
{
if(!trigger.new.isEmpty()){
for(Contact con : trigger.new)
{
if(con.AccountId!= trigger.oldMap.get(con.Id).AccountId){
if(con.AccountId == null && trigger.oldMap.get(con.Id).AccountId != null){
accconmap2.put(con.id,trigger.oldMap.get(con.Id).AccountId);
accIds.add(trigger.oldMap.get(con.Id).AccountId);
}else if(con.AccountId != null && trigger.oldMap.get(con.Id).AccountId == null){
accconmap.put(con.id,con.AccountId);
accIds.add(con.AccountId);
}
}
}
}
}
if(trigger.isAfter && trigger.isDelete)
{
if(!trigger.old.isEmpty())
{
for(Contact con : trigger.old)
{
if(con.AccountId !=null)
{
accIds.add(con.AccountId);
accconmap2.put(con.id,con.AccountId);
}
}
}
}
List<Account> accountToBeUpdated = new List<Account>();
if(!accIds.isEmpty())
{
List<Account> acclist = [Select Id, Number_of_Contacts__c from Account where Id IN : accIds];
if(!acclist.isEmpty()){
for(Account acc: acclist){
if( accconmap.values().contains(acc.id)){
if(!(accconmap1.containsKey(acc.id))){
Account a=new Account();
a.id=acc.id;
if(acc.Number_of_Contacts__c==null || acc.Number_of_Contacts__c == 0)
{
a.Number_of_Contacts__c=1;
}
else if(acc.Number_of_Contacts__c >= 1)
{
a.Number_of_Contacts__c =acc.Number_of_Contacts__c+1;
}
accconmap1.put(a.id,a);
}else{
Account a1=accconmap1.get(acc.id);
a1.Number_of_Contacts__c += 1;
accconmap1.put(a1.id,a1);
}
}
if( accconmap2.values().contains(acc.id)){
if(!(accconmap1.containsKey(acc.id))){
Account a=new Account();
a.id=acc.id;
if(acc.Number_of_Contacts__c!=null && acc.Number_of_Contacts__c>=1)
{
a.Number_of_Contacts__c=acc.Number_of_Contacts__c-1;
}
accconmap1.put(a.id,a);
}else{
Account a=accconmap1.get(acc.id);
a.Number_of_Contacts__c -=1;
accconmap1.put(a.id,a);
}
}
}
}
}
if(!accconmap1.values().isEmpty()){
update accconmap1.values();
}
}
=========================================================
Test Class with 89 percent coverage :
==============================================================
@istest//(seealldata=true)
private class ContactTriggerTest {
@isTest static void testName(){
Account testOpportunity = new Account(Name = 'Test Opp');
insert testOpportunity;
Contact cont = new Contact();
cont.FirstName='Test';
cont.LastName='Test24211';
cont.Cost__c=300;
cont.Accountid= testOpportunity.id;
insert cont;
Contact cont1 = new Contact();
cont1.FirstName='Test';
cont1.LastName='Test12';
cont1.Cost__c=200;
cont1.Accountid= testOpportunity.id;
insert cont1;
cont1.Cost__c=100;
update cont1;
cont1.Accountid= null;
update cont1;
cont1.Accountid= testOpportunity.id;
update cont1;
delete cont1;
undelete cont1;
}
@isTest static void testName1(){
Account testOpportunity = new Account(Name = 'Test Opp1');
insert testOpportunity;
Contact cont1 = new Contact();
cont1.FirstName='Test1';
cont1.LastName='Test1';
cont1.Cost__c=200;
cont1.Email='msivareddysfdc@gmail.com';
cont1.Accountid= testOpportunity.id;
insert cont1;
Contact cont = new Contact();
cont.FirstName='Test';
cont.LastName='Test';
cont.Cost__c=300;
cont.Email='msivareddysfdc1@gmail.com';
cont.Accountid= testOpportunity.id;
insert cont;
try{
cont.LastName=null;
testOpportunity.Name=null;
update testOpportunity;
update cont;
}catch(Exception e){
}
}
}
==================================================================
Comments
Post a Comment