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
Shubham Joge 9Shubham Joge 9 

custome rollup

roll-up summary to calculate total amount of child on case parent object 
i am having one standard case object on which case are created .Case object is have one lookup filed ie [parent case]
now i have to write one trigger on case object  parent case should have the cout of no of child object .
can anyone please help me with this trigger?
 
sachinarorasfsachinarorasf
Hi Shubham,

You should change the following code according to your requirement as object and fields
 
trigger RollupSummaryLogic on Opportunity (After insert,After delete,After Update) {
    if(Trigger.isAfter && Trigger.isInsert){
        RollupSummary.countMethod(trigger.new);
    }
    if(Trigger.isAfter && Trigger.isDelete)
    {
        RollupSummary.countMethod(trigger.old);
    if(Trigger.isAfter && Trigger.isUpdate)
    {
        RollupSummary.countMethod(trigger.old);
    }
    
}
public class RollupSummary {
 public static void countMethod(List<Opportunity> opportunityList){
        try{
            Set<Id> accountIdSet = new Set<Id>();
            for(Opportunity opportunityObj : opportunityList){
                accountIdSet.add(opportunityObj.AccountId);
            }
            List<Account> accountList = new List<Account>();
            accountList =[Select Number_of_Opportunity__c,Id,Name,
                          (Select Name from Opportunities)
                          from Account where Id In :accountIdSet Limit 50000];
            for(Account accountObj : accountList){
                List<Opportunity> oppotunityList=accountObj.Opportunities;
                accountObj.Number_of_Opportunity__c =oppotunityList.size();
            }
            System.debug('accountList'+accountList);
            update accountList;
        }catch(Exception ex){
            System.debug('Error is ::'+ Ex.getMessage() +' On Line Number is :::' + Ex.getLineNumber());
        }
    }
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora