Saturday, 10 March 2012

Rollup Summary using Trigger


//Rollup summary using trigger on conatct
//This is used for  displaying the number of contacts on account

trigger countingcontacsofacc on Contact (after insert,after delete,after update) {
map<id,list<contact>> mapcontactcount=new map<id,list<contact>>();
map<id,Account> accupdate = new map<id,account>();
for(Account acc:[select id,(select id,name from contacts) from account]){
        mapcontactcount.put(acc.id,acc.contacts);
        accupdate .put(acc.id,acc);
}

for(id c:mapcontactcount.keyset()){
    accupdate.get(c).NoofContacts__c=0;
    if(accupdate.get(c)!=null){
        accupdate.get(c).NoofContacts__c=mapcontactcount.get(c).size();
      }
}
     if(accupdate.size()>0)
update accupdate.values();

}

No comments:

Post a Comment

Get the Developer Name for Record Types without SOQL query

Hi, Previously, the developer name was accessible only via SOQL on the RecordType SObject, and not via describe information. Now you can ...