when we delete a quotelineitem, it should update Quote Record. My trigger works upto 2 records, when i delete last record and there is no more quotelineitem in Quote. it does not make the value of field 0 in quote. it shows the last value in Quote. kindly have a look.
My Trigger :
trigger SumQuote on QuoteLineItem (before insert, after insert, before update, after update, before delete, after delete) {
if (trigger.isafter && trigger.isdelete){
SumQuoteHandler.sumQuotefeld(trigger.old);
}
}
My apex handler Class :
public class SumQuoteHandler {
Public static set<Id> ids = new set<Id>();
Public static Decimal amount = 0;
Public Static decimal amount1 = 0;
Public static decimal amount2 = 0;
Public static void sumQuotefeld(List<QuoteLineItem>QLL){
for(QuoteLineItem QLII : QLL){
ids.add(QLII.QuoteId);
}
system.debug(ids);
List<Quote> QI = [SELECT Id, Rabatt_der_einmaligen_Geb_hren__c, einmalige_Geb_hren__c, monatlicher_LP_Lizenzen__c FROM Quote where Id =:ids]; // where Id IN:ids
system.debug(QI);
List<QuoteLineItem> QL = [SELECT id, QuoteId,Produktkategorie__c, isdeleted, GesamtpreisNEU__c, Euro_Rabatt_del__c, ListPrice FROM QuoteLineItem WHERE QuoteId IN: QI ];
system.debug(QL);// line 17
for(QuoteLineItem QQ : QL){
for (Quote Q : QI){
// system.debug(QQ);
if(Q.id==QQ.QuoteId && Q.Id!=null && QQ.Produktkategorie__c!='Software Subscription'){
system.debug(QQ);
amount += QQ.Euro_Rabatt_del__c;
amount1 += QQ.GesamtpreisNEU__c;
}
else if(Q.id==QQ.QuoteId && Q.Id!=null && QQ.Produktkategorie__c=='Software Subscription'){
amount2 += QQ.ListPrice;
}
Q.Rabatt_der_einmaligen_Geb_hren__c = amount;
Q.einmalige_Geb_hren__c = amount1;
Q.monatlicher_LP_Lizenzen__c = amount2;
update Q;
}
}
}
}
//QQ.Euro_Rabatt_del__c!=null && QQ.GesamtpreisNEU__c!=null &&
//&& QQ.ListPrice!=null
I shall be grateful to you for your kind cooperation. Thanks and regards
6 risposte
Something like that
// Declare the map
Map<Id, Quote> mapQuote = new Map<Id, Quote>();
for(QuoteLineItem QLII : QLL){
ids.add(QLII.QuoteId);
// Add 0 values to the map
mapQuote.put(QLII.QuoteId, new Quote(Id = QLII.QuoteId, Rabatt_der_einmaligen_Geb_hren__c = 0, einmalige_Geb_hren__c = 0, monatlicher_LP_Lizenzen__c = 0));
}
for(QuoteLineItem QQ : QL){
if(QQ.Produktkategorie__c != 'Software Subscription'){
mapQuote.get(qq.QuoteId).Rabatt_der_einmaligen_Geb_hren__c +=QQ.Euro_Rabatt_del__c;
mapQuote.get(qq.QuoteId).einmalige_Geb_hren__c +=QQ.GesamtpreisNEU__c;
}
else if(QQ.Produktkategorie__c == 'Software Subscription'){
mapQuote.get(qq.QuoteId).monatlicher_LP_Lizenzen__c +=QQ.ListPrice;
}
}
update mapQuote.values();