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
RajKumaR MRajKumaR M 

How to Achieve roll up trigger for Parent and child opportunity?

Hi All,

I have scenario to calculate the sum of child amount in Parent opp.

I have created trigger its working for Insert and update its not  working in Isdelete();

Can anyone help on this?

The following code which is worked for isinsert() and isupdate() and not updated in Isdelete.

trigger opprollup on Opportunity (after insert, after update,after delete)
{
  
   Set<Id> oppIds = new Set<Id> ();
     Set<Id> poppIds = new Set<Id> ();
   Public List <Opportunity > opps = new List<Opportunity >();
     Public List <Opportunity > popps = new List<Opportunity >();
   List <AggregateResult> groupedResults  = new List<AggregateResult>();
   
    
if(trigger.isInsert || trigger.isUpdate)
{
           for(Opportunity opp:trigger.new)
           {
             oppIds.add(opp.Parent_Opportunity__c );
            }
            opps = [Select Id,amount From Opportunity  Where Id In :oppIds  ];
            groupedResults  = [SELECT SUM(Amount) FROM Opportunity where Parent_Opportunity__c IN:oppIds];

  for(integer j=0; j<groupedResults  .size(); j++ )
  
       {
         for(integer i=0; i<opps  .size(); i++ )
         {  
         opps[i].Child_Total_Amount__c=(Decimal) groupedResults[j].get('expr0');
         }
  
        }
  
   update(opps );


  }
 
if(Trigger.IsDelete )
{

          
            popps = [Select Id,amount From Opportunity  Where Id In :oppIds  ];
            groupedResults  = [SELECT SUM(Amount) FROM Opportunity where Parent_Opportunity__c IN:oppIds];

  for(integer j=0; j<groupedResults  .size(); j++ )
  
       {
         for(integer i=0; i<popps  .size(); i++ )
         {
         System.Debug('VALUEEEEEEEEEEEEEEEEE'+ popps[0].amount);
         opps[i].Child_Total_Amount__c=(Decimal) groupedResults[j].get('expr0')-popps[0].amount;
         }
  
        }
  
   update(opps );



}
 

}




Thanks in advance!
BALA_RAMBALA_RAM
trigger count_of_contacts_to_relatedAccounts on Contact (after insert,after delete) {

   Set<id> accids = new set<id>();

    List<account> acclist = new List<account>();
    List<account> listacc = new List<account>();

    List<Contact> conList =new List<contact>();
    List<Contact> Listcon =new List<contact>();
   
  map<id,decimal> mapcount = new Map<id,decimal>();
 
  if(trigger.Isinsert)
  {
    for(contact c :Trigger.new)
    {
     accids.add(c.accountId);
    }
  }
  if(trigger.isDelete)
  {
   for(contact c :Trigger.old)
   {
    accids.add(c.accountId);
   }
  }
   acclist = [SELECT id,Name FROM Account WHERE ID IN:accids];
   conList = [SELECT id,AccountId FROM Contact WHERE AccountId IN:accids];
 
  for(Account acc:acclist)
  {
   Listcon.Clear();
   for(contact co :conList)
   {
    if(co.AccountId == acc.id){
     Listcon.add(co);
     mapcount.put(co.accountId,Listcon.size());
    }
   }
  }
  if(acclist.size() > 0)
  {
   for(Account ac :acclist)
   {
    if(mapcount.get(ac.Id)== NULL)
    {
     ac.count_of_contacts__c =0;
    }
    else
    ac.count_of_contacts__c = mapcount.get(ac.Id);
    listacc.add(ac);
   }
  }
  if(listacc.size() >0)
  {
   Update listacc;
  }
}

above trigger was just reference for caliculate the count or child records ...
emily_pan1.3928365784526E12emily_pan1.3928365784526E12
when Trigger.IsDelete, oppIds is empty. You could grab oppIds from Trigger.old.
BALA_RAMBALA_RAM
Hi emily_pan1,

Here Whenever child Opportunity is deleted at that time Amount will be changed on Parent Opportunity ..... :) try my trigger instead of Contact Use Opportunity.try on Amount field

thanks
Nirmal ChristopherNirmal Christopher
Where is your trigger.old condition. You have to iterate through trigger.old and get the amount value from child records and update it in parent.