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
Rajesh Singh 88Rajesh Singh 88 

Apex Class/Trigger : Calculate the sum of the 'Amount' of QuoteLineItems on the custom field 'Total Amount' on the opportunity.

I think this can be resolved using roll-up summary fields but my organization wants to go with Apex. I am a newbie to Apex. Can anyone please help me on this. I tried in Apex but i was able to update the Total Amount field with the sum amount value of quotelineitem related to a single quote , which will be completely wrong as quote is having masterdetail relationship with opportunity 

Objects/fields involved :
Opportunity (Total_Amount__c)
Quote : Masterdetail relationship with Opportunity
QuoteLineItem (Amount__c) : Masterdetail with Quote

What I excatly want to achieve is for example Opportunity is having 2 quotes and these quotes are having 3 quoteline items each , I want the sum of all the quotelineitems to be updated in the Total Amount field on Opportunity.


 
David Zhu 🔥David Zhu 🔥
You may use the following sample as reference:
AggregateResult[] groupedResults = [select sum(amount__c) from quotelineitem where quote.opportunity.id ='0068A000007ugyU'];
decimal totalAmount = decimal.valueof(groupedResults[0].get(0));

then assign totalAmount to quantity.total_amount__c field.
Rajesh Singh 88Rajesh Singh 88
@David Zhu , Thanks for the refernce. I wrote the below piece of code as suggested but getting the below error so can you please help me out to resolve the error.

Error:Apex trigger QuoteLineItem caused an unexpected exception, contact your administrator: QuoteLineItem: execution of AfterUpdate caused by: System.SObjectException: Invalid field Quote.opportunityId for AggregateResult: Trigger.QuoteLineItem: line 21, column 1

trigger QuoteLineItem on QuoteLineItem (after insert, after update) {
    set<id> quoteIdset =new set<id>();
    
    for(QuoteLineItem qol:trigger.new){
        quoteIdset.add(qol.Quoteid); //quoteId
    }
    
    List<opportunity> oppListtoUpdt=new list<opportunity>();
    set<id> oppIdSet=new set<id>();
    
    map<id,Quote> mapofQuote=new map<id,Quote>([select id,opportunityid from Quote where id=:quoteIdset]);
    
    for(Quote quoteRec:mapofQuote.values()){
        oppIdSet.add(quoteRec.opportunityid);
    }
    
    Map<ID, opportunity> mapOppty = new Map<ID, opportunity>([select id,Gross_Amount__c from opportunity where id=:oppIdSet]);
    AggregateResult[] groupedResults = [SELECT Quote.opportunityId,SUM(Amount__c)amt FROM 
                                        QuoteLineItem where Quote.opportunityId in:oppIdSet group by Quote.opportunityId];
    for(AggregateResult Results: groupedResults){
        Id opptyId =(id) Results.get('Quote.opportunityId');
        Opportunity opp=mapOppty.get(opptyId);
        opp.Gross_Amount__c=(integer)Results.get('amt');
        oppListtoUpdt.add(opp);
    }
    update oppListtoUpdt;
}

 
David Zhu 🔥David Zhu 🔥
map<id,Quote> mapofQuote=new map<id,Quote>([select id,opportunityid from Quote where id   IN :quoteIdset]);

Map<ID, opportunity> mapOppty = new Map<ID, opportunity>([select id,Gross_Amount__c from opportunity where id  IN  oppIdSet]);
Rajesh Singh 88Rajesh Singh 88
@David , Thanks for correcting. I am still getting the same error and it's coming for the below line:

for(AggregateResult Results: groupedResults){
        Id opptyId =(id) Results.get('Quote.opportunityId');  // The error is pointing out to this line
        Opportunity opp=mapOppty.get(opptyId);
        opp.Gross_Amount__c=(integer)Results.get('amt');
        oppListtoUpdt.add(opp);
    }
David Zhu 🔥David Zhu 🔥
Id opptyId =(id) Results.get(0);  
Rajesh Singh 88Rajesh Singh 88
Basically I have to get the ID of the opportunity so that I can update or map the sum(Amount__c) of all the QuotelineItems to the specific Opportunity record
Rajesh Singh 88Rajesh Singh 88
Can anyone please help me on the above, I have tried everything but wasn't able to get the expected result.

Thanks!