Make three fields on account page naming Total 'Contacts','total male contacts','total female contacts' and make update these fields by coding a trigger on contact page ?
--------------------------------------------------------------------------------------------
trigger ContactTrigger on Contact(after insert, after update,after delete, before delete, before insert, before update) {
ContactTriggerHandler triggerHandler = new ContactTriggerHandler(Trigger.isExecuting,Trigger.size);
// if(Trigger.isBefore && Trigger.isInsert){
// triggerHandler.OnBeforeInsert(trigger.new, trigger.NewMap);
// }
if(Trigger.isAfter && Trigger.isInsert){
triggerHandler.OnAfterInsert(trigger.new, trigger.NewMap);
//system.debug('after insert'+trigger.New);
}
// if(Trigger.isBefore && Trigger.isUpdate){
// triggerHandler.OnBeforeUpdate(trigger.new, trigger.NewMap, Trigger.oldMap);
//}
//if(Trigger.isAfter && Trigger.isUpdate){
// triggerHandler.OnAfterUpdate(trigger.new, trigger.NewMap, Trigger.oldMap);
//}
//if(Trigger.isBefore && Trigger.isDelete){
// system.debug('before delete'+trigger.Old+''+trigger.OldMap);
//triggerHandler.OnBeforeDelete(trigger.Old, Trigger.oldMap);
//}
system.debug('Trigger.isAfter='+Trigger.isAfter+'Trigger.isDelete='+Trigger.isDelete);
if(Trigger.isAfter && Trigger.isDelete){
system.debug('after delete');
triggerHandler.OnAfterDelete(trigger.Old, Trigger.oldMap);
system.debug(trigger.Old+'krish'+trigger.OldMap);
}
}
========================================================================
public class ContactTriggerHandler
{ private boolean m_isExecuting = True;
private integer BatchSize = 0;
public ContactTriggerHandler(boolean isExecuting, integer size)
{
m_isExecuting = isExecuting;
BatchSize = size;
}
/* public void onBeforeInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
}
public void OnBeforeUpdate(list<Contact> triggerNew, map<id,Contact> triggerNewMap, map<id,Contact> triggerOldMap)
{
}*/
public void OnAfterInsert(list<Contact> triggerNew, map<id,Contact> triggerNewMap)
{
updateNumberOfGenderContactsOnAccount(triggerNew, triggerNewMap);
updateNumberOfContactsOnAccount(triggerNew, triggerNewMap);
}
public void OnBeforeDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
}
public void OnAfterDelete(list<Contact> triggerOld, map<id,Contact> triggerOldMap)
{
// system.debug('after delete class');
updateNumberOfContactsOnAccount(triggerold, triggeroldMap);
updateNumberOfGenderContactsOnAccount(triggerOld, triggerOldMap);
}
private void updateNumberOfContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
setOfAccountIdFromContact.add(eachCon.AccountId);
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
list<Account> lstAccount = [select id, Name, Total_Contacts__c,
(select id from contacts)
from Account
where Id IN : setOfAccountIdFromContact];
if(lstAccount != null && lstAccount.size() > 0)
{
for(Account eachAccount : lstAccount)
{
eachAccount.Total_Contacts__c = eachAccount.contacts.size();
//eachAccount.Name += '_1';
}
update lstAccount;
}
}
}
private void updateNumberOfGenderContactsOnAccount(List<Contact> triggerNew,Map<id,Contact> triggerNewMap)
{
Set<id> setOfAccountIdFromContact=new Set<id>();
Set<String> setOfGenderFromContact=new Set<String>();
if(triggerNew!=null&&triggerNew.size()>0)
{
for(Contact eachCon : triggerNew)
{
if(eachCon.AccountId != null)
{
setOfAccountIdFromContact.add(eachCon.AccountId);
setOfgenderFromContact.add(eachCon.Gender__c);
}
}
}
if(setOfAccountIdFromContact != null && setOfAccountIdFromContact.size() > 0)
{
for(String st:setOfgenderFromContact)
{
List<Account> lstAccount = [select id, Name, Total_Contacts__c,Total_male_Contacts__c,(select id from contacts where gender__c IN:setOfgenderFromContact) from Account
where Id IN : setOfAccountIdFromContact];
if(st=='male')
{
for(Account objAccount :lstAccount)
{
objAccount.Total_Male_Contacts__c=objAccount.contacts.size();
}
}
if(st=='female')
{
for(Account objAccount :lstAccount)
{
objAccount.Total_Female_Contacts__c=objAccount.contacts.size();
}
}
update lstAccount;
}
}
}
}
No comments:
Post a Comment