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
mustapha laouari 15mustapha laouari 15 

Sum() provides previous result instead of new one.

Hello everyone,

In QuoteLineItem, i have a custom field : Benefit
Into Quote i have a custom field : total Benefit which is the Sum(Benefit)
Everything fine !
Each time I make a change on QuoteLineItem, i fire a trigger (AFTER insert/Update) in charge to copy the Sum(Benefit) result into the Opportunity.
Problem is that on Opportunity I always have the Previous result of Sum(Benefit) ...

could you help please ?
Thanks you
Amit Chaudhary 8Amit Chaudhary 8
You need to query the same field again to get the updated value

 
mustapha laouari 15mustapha laouari 15
To provide more details, here is the Trigger
trigger NkecQli on QuoteLineItem (before insert, before update, after insert, after update) {
    if (Trigger.isafter) {
        List<QuoteLineItem> Qli = Trigger.new;
        System.debug('REQUESTING CopyBenefit');
        Myclass.CopyBenefit(Qli);
    }
 
public static void CopyBenefit(List<QuoteLineItem> QliList) {
       quote q = [SELECT IsSyncing, opportunityid, TotalBenefit from Quote Where Id=:QliList[0].id Limit 1];
       System.debug('quote' + q);
       if (q.IsSyncing)
       {opportunity opp = new opportunity();
        opp.id=q.OpportunityId;
        opp.TotalBenefit = q.TotalBenefit;
        update opp;
       }
   }
Thanks
 
mustapha laouari 15mustapha laouari 15
Hi Amit, thanks for answer,
im  not sure to understand what you mean...
Ashish DevAshish Dev
Probably you need to split your code and put oppty related code in oppty trigger only and not in QuoteLineItem.
mustapha laouari 15mustapha laouari 15
Thanks,
Exatly the same result ;-( 
No sense, i beleave the question is when is the Sum() calculated and stored ?