You need to sign in to do that
Don't have an account?
Rajesh 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.
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.
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.
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;
}
Map<ID, opportunity> mapOppty = new Map<ID, opportunity>([select id,Gross_Amount__c from opportunity where id IN oppIdSet]);
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);
}
Thanks!