function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SidbhtSidbht 

Hi There, Can anyone help me with rollup summary trigger on Accounts and Order. I want to populate the Count of Orders on Account and when Order is deleted Account Count should update. Thanks in advance.

Hi There,  Can anyone help me with rollup summary trigger on Accounts and Order. I want to populate the Count of Orders on Account and when Order is deleted Account Count should update. Thanks in advance.
Best Answer chosen by Sidbht
Harish RamachandruniHarish Ramachandruni
Hi,


This is sample code count of contact for account  :

and change to as you required.
 
trigger CountContactsnew on Contact (after insert, after delete, after undelete) {

    List<id> accIdList = new List<id>();
    if(Trigger.isInsert || Trigger.isUndelete){
        For(Contact con1 : Trigger.new){
            accIdList.add(con1.accountid);
        }
    }
    if(Trigger.isDelete){
        For(Contact con1 : Trigger.old){
            accIdList.add(con1.accountid);
        }
    }
    List<Account> accUpdateList = new List<Account>();
    For(Account acc : [SELECT Contact_Recs__c,(SELECT id FROM Contacts) FROM Account WHERE id =: accIdList]){
        acc.Contact_Recs__c = acc.Contacts.size();
        accUpdateList.add(acc);
    }
    try{
        update accUpdateList;
    }Catch(Exception e){
        System.debug('Exception :'+e.getMessage());
    }
}


Thanks,
Harish R.
 

All Answers

Harish RamachandruniHarish Ramachandruni
Hi,


This is sample code count of contact for account  :

and change to as you required.
 
trigger CountContactsnew on Contact (after insert, after delete, after undelete) {

    List<id> accIdList = new List<id>();
    if(Trigger.isInsert || Trigger.isUndelete){
        For(Contact con1 : Trigger.new){
            accIdList.add(con1.accountid);
        }
    }
    if(Trigger.isDelete){
        For(Contact con1 : Trigger.old){
            accIdList.add(con1.accountid);
        }
    }
    List<Account> accUpdateList = new List<Account>();
    For(Account acc : [SELECT Contact_Recs__c,(SELECT id FROM Contacts) FROM Account WHERE id =: accIdList]){
        acc.Contact_Recs__c = acc.Contacts.size();
        accUpdateList.add(acc);
    }
    try{
        update accUpdateList;
    }Catch(Exception e){
        System.debug('Exception :'+e.getMessage());
    }
}


Thanks,
Harish R.
 
This was selected as the best answer
Duke_SfdcDuke_Sfdc
Hi Sidbht

Please see the below code:

Trigger count_no_of_Orders on Order (after insert,after delete,after update,after undelete) {
    
    set<id> accountIdSet=new set<id>();
    
    for(order od:trigger.isDelete?trigger.old:trigger.new){
         if(od.accountid!=null){
            accountIdSet.add(od.accountid);
        }
    }
    map<string,integer> mapdata=new map<string,integer>();
    for(AggregateResult ar:[select accountid ai,count(id) oid from order where check__c=true and accountid in:accountIdSet group by accountid]){
        mapdata.put(string.valueof(ar.get('ai')), integer.valueOf(ar.get('oid')));
    }
    list<account> acclist=new list<account>();
    for(string ids:accountIdSet){
        integer n=0;
        if(mapdata.containsKey(ids))
            n=mapdata.get(ids);
        account acc=new account(id=ids,Number_Of_Orders__c=n);
            acclist.add(acc);
    }
    
    if(acclist.size()>0)
        dataBase.update(accList);
    
}

Please mark your problem as solved if this will fulfill your requirement.

Thanks,
Duke_Sfdc