You need to sign in to do that
Don't have an account?
uday uday chavan
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);
}
}
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);
}
}
Please try the below trigger and helper class:
Apex Trigger:
Apex Class:
Thanks,
Maharajan.C
All Answers
you can refer below code
-
to
Please try the below trigger and helper class:
Apex Trigger:
Apex Class:
Thanks,
Maharajan.C