[SalesForce] Rollup Summary Trigger when I change the parent record

Hi this is my trigger for Rollup summary. No problem it is working how i wanted it to be but the problem is when i change the parent name it is not updating the field value in new parent record Kindly tell me where exactly i need to do changes.

Here Region_Target__c is a parent object and Salesorder is a child object. Q1_Achived__c is the field i'm trying to update using roll up trigger, When i change parent name in the new parent record Q1_Achived__c field is not updating automatically.

trigger target_calculation12 on Sales_Order__c (before insert, after insert, before update, 
    after update, after delete, after undelete) 
{
    Set<Id> accid = new Set<Id>();
    List<Region_Target__c> Acnt = new List<Region_Target__c>();

    Sales_Order__c[] enq;

    if(Trigger.isdelete || Trigger.isupdate )
    { 
        enq = Trigger.old; 
    }
    else
    {
        enq = Trigger.new;
    }

    for(Sales_Order__c opp:enq)
    {
        accid.add(opp.Region_Target__c);
    }

    for(Region_Target__c acc:[SELECT Id,Q1_Achived__c FROM Region_Target__c where id=:accid])
    {
        acc.Q1_Achived__c=0;

        for(Sales_Order__c opp:[SELECT Region_Target__c,Amount__c,Stage__c,
            Id,quarter__c FROM Sales_Order__c 
            where Region_Target__c = :accid and stage__c = 'Won' and quarter__c = 2 ])
        {
            acc.Q1_Achived__c = acc.Q1_Achived__c + opp.Amount__c;
        }
        update acc;
    }
}

Thank you very much in advance.

Best Answer

The main issue that you're facing is the SOQL statement inside of your for loop, which is a sure fire way to run into limits in triggers.

Beyond that though, I can't quite understand what you're trying to accomplish. YTD_Booking__c is always getting reset to 0 and then reset to the latest opp amount. It's not actually summing the opps at all, but each time you go through the loop, you are setting the field to the latest opp amount. What you really want here can be accomplished without apex, as far as I can tell from your requirements.

You could create a rollup summary field that sums the amount of 'won' opportunities related to an account.

If you still feel that you need to do this in apex, you should consider an aggregate query.

AggregateResult[] groupedResults = [SELECT AccountId, SUM(Amount) bookingTotal
    FROM Opportunity 
    WHERE Stage__c = 'won'
    GROUP BY AccountId];
for (AggregateResult ar : groupedResults)  {
    System.debug('Account Id' + ar.get('AccountId'));
    System.debug('Total Bookings' + ar.get('bookingTotal'));
}

There are also some other curiosities worth calling out. Why are you using a custom stage field on opportunity, where there is a native stage field (StageName). Also, You don't need to create a set of Ids from the trigger set, your Query WHERE clause can use trigger.new or trigger.old but you need to use the IN clause instead of equals:

for(Account acc:[SELECT Id,YTD_Booking__c FROM Account where id IN : enq])
Related Topic