+ Start a Discussion
uday uday chavanuday uday chavan 

after update using roll up summary logic


this is my code i want to give count of contacts associate with account but when i changed contacts account(lookup) it update new owner count value but not updating the value in old account count

this is my code
handler:
public class ContactRollupSummaryHandler {
    public static void afterInsert(list<Contact> contacts){
        NumberofContacts(contacts);
    }
    public static void afterUpdate(list<Contact> contacts,Map<Id,Contact> oldMap){
        list<Contact> conlist = new list<Contact>();
        for(Contact c: contacts){
            if(c.AccountId != oldMap.get(c.Id).AccountId){
                conlist.add(c);
            }
        }
        if(conlist.size()>0){
            NumberofContacts(conlist);
        }
    }
    public static void afterDelete(list<Contact> contacts){
        NumberofContacts(contacts);
    }
    public static void afterUndelete(list<Contact> contacts){
        NumberofContacts(contacts);
    }
    
    
    public static void NumberofContacts(list<Contact> con){
        set<id> accIds = new set<id>();
        for(Contact c: con){
            if(c.AccountId != null){
            accIds.add(c.AccountId);
            } 
        }
        
        if(accIds.size()>0){
            list<Account> acclist = new list<Account>();
        for(Account ac: [SELECT id,Number_of_Contacts__c,(SELECT id from Contacts) from Account where id IN:accIds]){
            ac.Number_of_Contacts__c = ac.Contacts.size();
            acclist.add(ac);
        }
            if(acclist.size()>0){
                update acclist;
            }    
        
        }
    }

}

Trigger:
trigger ContactTrigger on Contact (after insert,after update,after delete, after undelete) {
    if(Trigger.isAfter && Trigger.isInsert){
       ContactRollupSummaryHandler.afterInsert(Trigger.new);
    }
    if(Trigger.isAfter && Trigger.isUpdate){
       ContactRollupSummaryHandler.afterUpdate(Trigger.new,Trigger.oldMap);
    }
    if(Trigger.isAfter && Trigger.isDelete){
       ContactRollupSummaryHandler.afterDelete(Trigger.old);
    }
    if(Trigger.isAfter && Trigger.isUndelete){
       ContactRollupSummaryHandler.afterUndelete(Trigger.new);
    }
    
}
Best Answer chosen by uday uday chavan
Maharajan CMaharajan C
Hi Uday,

Please try the below trigger and helper class:

Apex Trigger:
 
trigger ContactTrigger on Contact (after insert,after update,after delete, after undelete) {
    if(Trigger.isAfter && Trigger.isInsert){
        ContactRollupSummaryHandler.afterInsert(Trigger.new);
    }
    if(Trigger.isAfter && Trigger.isUpdate){
        ContactRollupSummaryHandler.afterUpdate(Trigger.new,Trigger.oldMap);
    }
    if(Trigger.isAfter && Trigger.isDelete){
        ContactRollupSummaryHandler.afterDelete(Trigger.old);
    }
    if(Trigger.isAfter && Trigger.isUndelete){
        ContactRollupSummaryHandler.afterUndelete(Trigger.new);
    }
}

Apex Class:
 
public class ContactRollupSummaryHandler  {
    public static void afterInsert(List<contact> contacts){
        set<id> accIds = new set<id>();
        for(Contact c: contacts){
            if(c.AccountId != null){
                accIds.add(c.AccountId);
            } 
        }
        
        if(!accIds.IsEmpty())
            NumberofContacts(accIds);
    }
    
    public static void afterUpdate(List<contact> contacts, Map<Id,Contact> conMap){
        set<id> accIds = new set<id>();
        for(Contact c: contacts){
            if(c.AccountId != conMap.get(c.Id).AccountId){
                accIds.add(c.AccountId);
                accIds.add(conMap.get(c.Id).AccountId);
            } 
        }
        
        if(!accIds.IsEmpty())
            NumberofContacts(accIds);
    }
    
    public static void afterDelete(List<contact> contacts){
        set<id> accIds = new set<id>();
        for(Contact c: contacts){
            if(c.AccountId != null){
                accIds.add(c.AccountId);
            } 
        }
        
        if(!accIds.IsEmpty())
            NumberofContacts(accIds);
    }
    
    public static void afterUndelete(List<contact> contacts){
        set<id> accIds = new set<id>();
        for(Contact c: contacts){
            if(c.AccountId != null){
                accIds.add(c.AccountId);
            } 
        }
        
        if(!accIds.IsEmpty())
            NumberofContacts(accIds);
    }
    
    public static void NumberofContacts(set<Id> accIds){
        list<Account> acclist = new list<Account>();
        for(Account ac: [SELECT id,Number_of_Contacts__c,(SELECT id from Contacts) from Account where id IN:accIds]){
            ac.Number_of_Contacts__c = ac.Contacts.size();
            acclist.add(ac);
        }
        if(acclist.size()>0){
            update acclist;
        }   
    }
}

Thanks,
Maharajan.C

All Answers

Danish HodaDanish Hoda
hi Uday,
you can refer below code
public static void afterInsert(list<Contact> contacts){
	NumberofContacts(contacts, null);
}
public static void afterUpdate(list<Contact> contacts,Map<Id,Contact> oldMap){
	list<Contact> conlist = new list<Contact>();
	for(Contact c: contacts){
		if(c.AccountId != oldMap.get(c.Id).AccountId){
			conlist.add(c);
		}
	}
	if(conlist.size()>0){
		NumberofContacts(conlist, oldMap);
	}
}
public static void afterDelete(list<Contact> contacts,Map<Id,Contact> oldMap){
	NumberofContacts(contacts, oldMap);
}
public static void afterUndelete(list<Contact> contacts){
	NumberofContacts(contacts, null);
}

public static void NumberofContacts(list<Contact> con, Map<Id,Contact> oldMap){
	set<id> accIds = new set<id>();
	for(Contact c: con){
		if(c.AccountId != null){
			accIds.add(c.AccountId);
		}
		if(oldMap != null && oldMap.get(c.Id).AccountId != c.AccountId) {
			accIds.add(c.AccountId);
		}
	}
	
	if(accIds.size()>0){
		list<Account> acclist = new list<Account>();
		for(Account ac: [SELECT id,Number_of_Contacts__c,(SELECT id from Contacts) from Account where id IN:accIds]){
			ac.Number_of_Contacts__c = ac.Contacts.size();
			acclist.add(ac);
		}
		if(acclist.size()>0){
			update acclist;
		}    
	
	}
}


 
uday uday chavanuday uday chavan
hello danish this is still not working
Danish HodaDanish Hoda
any error you got Uday?
Danish HodaDanish Hoda
Plz modify this piece of code 
public static void afterUpdate(list<Contact> contacts,Map<Id,Contact> oldMap){
	list<Contact> conlist = new list<Contact>();
	for(Contact c: contacts){
		if(c.AccountId != oldMap.get(c.Id).AccountId){
			conlist.add(c);
		}
	}
	if(conlist.size()>0){
		NumberofContacts(conlist, oldMap);
	}
}

to
public static void afterUpdate(list<Contact> contacts,Map<Id,Contact> oldMap){
		NumberofContacts(contacts, oldMap);
}


 
uday uday chavanuday uday chavan
still not working 
uday uday chavanuday uday chavan
the old field count of is not updating it remains same as it
Danish HodaDanish Hoda
Can you try adding the null  check - 
if(accIds.size()>0){
		list<Account> acclist = new list<Account>();
		for(Account ac: [SELECT id,Number_of_Contacts__c,(SELECT id from Contacts) from Account where id IN:accIds]){
			if(!ac.Contacts.isEmpty()){
				ac.Number_of_Contacts__c = ac.Contacts.size();
				acclist.add(ac);
			}else{
				ac.Number_of_Contacts__c = 0;
				acclist.add(ac);
			}
		}
		if(acclist.size()>0){
			update acclist;
		}    
	
	}
Maharajan CMaharajan C
Hi Uday,

Please try the below trigger and helper class:

Apex Trigger:
 
trigger ContactTrigger on Contact (after insert,after update,after delete, after undelete) {
    if(Trigger.isAfter && Trigger.isInsert){
        ContactRollupSummaryHandler.afterInsert(Trigger.new);
    }
    if(Trigger.isAfter && Trigger.isUpdate){
        ContactRollupSummaryHandler.afterUpdate(Trigger.new,Trigger.oldMap);
    }
    if(Trigger.isAfter && Trigger.isDelete){
        ContactRollupSummaryHandler.afterDelete(Trigger.old);
    }
    if(Trigger.isAfter && Trigger.isUndelete){
        ContactRollupSummaryHandler.afterUndelete(Trigger.new);
    }
}

Apex Class:
 
public class ContactRollupSummaryHandler  {
    public static void afterInsert(List<contact> contacts){
        set<id> accIds = new set<id>();
        for(Contact c: contacts){
            if(c.AccountId != null){
                accIds.add(c.AccountId);
            } 
        }
        
        if(!accIds.IsEmpty())
            NumberofContacts(accIds);
    }
    
    public static void afterUpdate(List<contact> contacts, Map<Id,Contact> conMap){
        set<id> accIds = new set<id>();
        for(Contact c: contacts){
            if(c.AccountId != conMap.get(c.Id).AccountId){
                accIds.add(c.AccountId);
                accIds.add(conMap.get(c.Id).AccountId);
            } 
        }
        
        if(!accIds.IsEmpty())
            NumberofContacts(accIds);
    }
    
    public static void afterDelete(List<contact> contacts){
        set<id> accIds = new set<id>();
        for(Contact c: contacts){
            if(c.AccountId != null){
                accIds.add(c.AccountId);
            } 
        }
        
        if(!accIds.IsEmpty())
            NumberofContacts(accIds);
    }
    
    public static void afterUndelete(List<contact> contacts){
        set<id> accIds = new set<id>();
        for(Contact c: contacts){
            if(c.AccountId != null){
                accIds.add(c.AccountId);
            } 
        }
        
        if(!accIds.IsEmpty())
            NumberofContacts(accIds);
    }
    
    public static void NumberofContacts(set<Id> accIds){
        list<Account> acclist = new list<Account>();
        for(Account ac: [SELECT id,Number_of_Contacts__c,(SELECT id from Contacts) from Account where id IN:accIds]){
            ac.Number_of_Contacts__c = ac.Contacts.size();
            acclist.add(ac);
        }
        if(acclist.size()>0){
            update acclist;
        }   
    }
}

Thanks,
Maharajan.C
This was selected as the best answer