0

Basically, I want to insert a billing line (for the tax) on my billing after inserting the billing (if there exists a tax rate on the account). Simple, however the calculation for this billing line depends on the total amount on billing (AcctSeed__Total__c).

Since the total is a roll up summary field, it is not updated (so the amount=0). How can I use trigger to achieve this? Can this be done using trigger.old? Will the roll up summary field be updated in trigger.old after insert trigger?

I know one approach is to up use AGGREGATE RESULT on my billing lines to get that total, but is there another way?

trigger CreateTaxLine on AcctSeed__Billing__c (after insert) {

List <id> bids= new List <id>();
List <AcctSeed__Billing_Line__c> blcs= new List<AcctSeed__Billing_Line__c> ();
for (AcctSeed__Billing__c b: Trigger.new) {
    bids.add(b.id);
}
// SOQL TO get billings with tax 
  List <AcctSeed__Billing__c> bls = [Select a.AcctSeed__Total__c,a.AcctSeed__Date__c, a.AcctSeed__Customer__r.Account_Tax__r.AcctSeed__Tax_Rate__c,a.AcctSeed__Customer__r.Account_Tax__r.AcctSeed__Rate__c, a.AcctSeed__Customer__c,
                                     a.AcctSeed__Customer__r.Account_Tax__r.AcctSeed__Tax_Rate__r.Id, a.AcctSeed__Customer__r.Account_Tax__r.AcctSeed__Tax_Rate__r.AcctSeed__Revenue_GL_Account__c
                                     From AcctSeed__Billing__c a 
                                     where a.AcctSeed__Customer__r.Account_Tax__r.AcctSeed__Tax_Rate__c!=null AND id=:bids  ];
   //New billing line for the billings that should have a tax item. 
    for (AcctSeed__Billing__c bl: bls){
        if(bl.AcctSeed__Customer__r.Account_Tax__r.AcctSeed__Rate__c!=null && bl.AcctSeed__Total__c!=null)
        {
            system.debug('**********'+bl.AcctSeed__Total__c);
            system.debug('********** TAX RATE'+bl.AcctSeed__Customer__r.Account_Tax__r.AcctSeed__Rate__c);
            AcctSeed__Billing_Line__c blc= new AcctSeed__Billing_Line__c ();
                blc.AcctSeed__Billing__c= bl.Id;
                blc.AcctSeed__Hours_Units__c= 1;
            blc.AcctSeed__Rate__c= (bl.AcctSeed__Total__c)*(bl.AcctSeed__Customer__r.Account_Tax__r.AcctSeed__Rate__c);
               blc.AcctSeed__Product__c= bl.AcctSeed__Customer__r.Account_Tax__r.AcctSeed__Tax_Rate__r.Id;
               blc.AcctSeed__Revenue_GL_Account__c= bl.AcctSeed__Customer__r.Account_Tax__r.AcctSeed__Tax_Rate__r.AcctSeed__Revenue_GL_Account__c;
            blc.AcctSeed__Date__c= bl.AcctSeed__Date__c;

             blcs.add(blc);


        }

    }
    //insert the billing lines
    insert (blcs);
}
0

I got this to work....the workaround was to have a conditional if statement... so before the roll-up field is populated (after the record is saved), it has a default value (0 in my case), so to prevent an infinite loop of (after update), I placed this condition: Trigger.oldmap.get(bls.id).AcctSeed__Total__c==0

Since total__c is my roll-up field for billing and after the record gets saved, its old value ==0, but after the second iteration of trigger, the old value gets updated, thus preventing an infinite self referential trigger. Cheers ! :)

| improve this answer | |
  • Just hope that no one creates a billing for a "zero" amount..lol. – Sammy7 Mar 9 '17 at 15:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.