You need to sign in to do that
Don't have an account?
Ankit Sehgal
Roll up Summary Trigger
I need to calculate the sum of tax field of all the quote line items which have taxable check box true and display it on the Quote field.The tax field on the line item is a formula field so i am unable to create a roll up summary.I wrote a trigger but it is failing in delete case.Can somebody guide me where i am goinge wrong with it
trigger CalculateTotalTax on QuoteLineItem (after insert,after update,after delete) { list<QuoteLineItem> l; if(Trigger.isInsert || Trigger.isUpdate) { l =[SELECT Id,QuoteId,Tax__c,Taxable__c FROM QuoteLineItem WHERE ID IN: Trigger.new]; } if(Trigger.isDelete) { l =[SELECT Id,QuoteId,Tax__c,Taxable__c FROM QuoteLineItem WHERE ID IN: Trigger.old]; } Decimal ttx=0.00; String qid; for(QuoteLineItem q : l) { qid=q.QuoteId; } for(QuoteLineItem f : l) { if(f.Taxable__c == true) { ttx+=f.Tax__c; } } Quote q=[SELECT Id,TotalTax__c FROM Quote WHERE Id =: qid]; q.TotalTax__c=ttx; System.debug(ttx); update q; }
But, If you looking for better code which handles bulk scenarois and is sclable, use the code below -
Use this utility class -
And update your trigger to as below -
Note - Mark this as best answer if it solves your problem.