• emily_pan1.3928365784526E12
  • NEWBIE
  • 0 Points
  • Member since 2014

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
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!