You need to sign in to do that
Don't have an account?

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!
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!
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 ...
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