ShowAll Questionssorted byDate Posted
RajKumaR 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)
{
}
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 );

}

}

BALA_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)
{
}
}
if(trigger.isDelete)
{
for(contact c :Trigger.old)
{
}
}
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){
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);
}
}
if(listacc.size() >0)
{
Update listacc;
}
}

above trigger was just reference for caliculate the count or child records ...
emily_pan1.3928365784526E12
when Trigger.IsDelete, oppIds is empty. You could grab oppIds from Trigger.old.
BALA_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 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.