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
Kevin Rutledge 4Kevin Rutledge 4 

Need help using an Apex Trigger to post billings and apply a cash receipt....

I am trying to use an Apex trigger to post billings and apply a cash receipt. I get the error, "Variable does not exist:bill" (it is on the 4th line from the bottom and in bold).
Does anyone have any suggestions?

trigger AcctSeed_CreateiDonateBilling on Opportunity (after insert,after update) {
    for (Opportunity Oppty : Trigger.New){
        IF(Oppty.I_Donate__c == TRUE && Oppty.StageName == 'Pledged'){
   
    // Create billing records to post and unpost
    
    AcctSeed__Billing__c[] billings = new List <AcctSeed__Billing__c> ();
        billings.add(
            new AcctSeed__Billing__c(
                AcctSeed__Date__c = System.today(),
                AcctSeed__Customer__c = Oppty.AccountId,
                AcctSeed__Status__c = 'Approved',
                AcctSeed__Due_Date2__c = System.today() + 30,
                AcctSeed__Opportunity__c = Oppty.Id
                )
            );

    insert billings;

    // Create billing line records to post and unpost
    AcctSeed__Billing_Line__c[] bLines = new List <AcctSeed__Billing_Line__c> ();
    AcctSeed__GL_Account__c glAccount = [Select Id From AcctSeed__GL_Account__c limit 1];
    
    for (AcctSeed__Billing__c bill : billings) {
        AcctSeed__Billing_Line__c objBillingLine = new AcctSeed__Billing_Line__c();
        objBillingLine.AcctSeed__Billing__c = bill.id;
        objBillingLine.AcctSeed__Date__c = System.today();
        objBillingLine.AcctSeed__Rate__c = Oppty.Amount;
        objBillingLine.AcctSeed__Hours_Units__c = 1;
        objBillingLine.AcctSeed__Revenue_GL_Account__c = glAccount.Id;
        bLines.add(objBillingLine);
        }
    
    insert bLines;
        
    // Call the post billings service
    AcctSeed.PostResult[] postResults = AcctSeed.BillingPostService.postBillings(billings);
    
    //Create Cash Receipt for Payment
    //   AcctSeed__Cash_Receipt__c[] CashRcpt = new List <AcctSeed__Cash_Receipt__c> ();
        AcctSeed__Cash_Receipt__c objCashReceipt = new AcctSeed__Cash_Receipt__c();
        objCashReceipt.AcctSeed__Amount__c = Oppty.Amount;
        objCashReceipt.AcctSeed__Account__c = Oppty.AccountId;
        objCashReceipt.AcctSeed__Payment_Reference__c = 'iDonate Pledge';
        objCashReceipt.AcctSeed__Receipt_Date__c = System.today();
         insert objCashReceipt;
     I have this Apex trigger but I keep getting an error       
            
        AcctSeed__Billing_Cash_Receipt__c     objBCR = new AcctSeed__Billing_Cash_Receipt__c();
        objBCR.AcctSeed__Billing__c = bill.id;
        objBCR.AcctSeed__Cash_Receipt__c = objCashReceipt.id;
        objBCR.AcctSeed__Applied_Amount__c = objCashReceipt.AcctSeed__Amount__c;
        insert objBCR;
Ravi Dutt SharmaRavi Dutt Sharma
Kevin,

bill is a local variable and will be available only inside the for loop. You need to move the last 5 lines inside the for loop. If you can post the complete trigger, maybe I can help you more. Thanks.
 
Deepali KulshresthaDeepali Kulshrestha
Hi Kevin,

Variable 'bill' is only usable in for loop but in you use it out of the loop that why you getting this error. Try the following code it may be helpful for you:
trigger AcctSeed_CreateiDonateBilling on Opportunity (after insert,after update) {
    for (Opportunity Oppty : Trigger.New){
        IF(Oppty.I_Donate__c == TRUE && Oppty.StageName == 'Pledged'){
    List<AcctSeed__Billing__c> billings = new List<AcctSeed__Billing__c>();
        billings.add(
            new AcctSeed__Billing__c(
                AcctSeed__Date__c = System.today(),
                AcctSeed__Customer__c = Oppty.AccountId,
                AcctSeed__Status__c = 'Approved',
                AcctSeed__Due_Date2__c = System.today() + 30,
                AcctSeed__Opportunity__c = Oppty.Id
                )
            );
    insert billings;
    AcctSeed__Billing_Line__c[] bLines = new List <AcctSeed__Billing_Line__c> ();
    AcctSeed__GL_Account__c glAccount = [Select Id From AcctSeed__GL_Account__c limit 1];
    for (AcctSeed__Billing__c bill : billings) {
        AcctSeed__Billing_Line__c objBillingLine = new AcctSeed__Billing_Line__c();
        objBillingLine.AcctSeed__Billing__c = bill.id;
        objBillingLine.AcctSeed__Date__c = System.today();
        objBillingLine.AcctSeed__Rate__c = Oppty.Amount;
        objBillingLine.AcctSeed__Hours_Units__c = 1;
        objBillingLine.AcctSeed__Revenue_GL_Account__c = glAccount.Id;
        bLines.add(objBillingLine);
        }
    insert bLines;
        
    AcctSeed.PostResult[] postResults =AcctSeed.BillingPostService.postBillings(billings);
    
        AcctSeed__Cash_Receipt__c objCashReceipt = new AcctSeed__Cash_Receipt__c();
        objCashReceipt.AcctSeed__Amount__c = Oppty.Amount;
        objCashReceipt.AcctSeed__Account__c = Oppty.AccountId;
        objCashReceipt.AcctSeed__Payment_Reference__c = 'iDonate Pledge';
        objCashReceipt.AcctSeed__Receipt_Date__c = System.today();
         insert objCashReceipt;
        AcctSeed__Billing_Cash_Receipt__c     objBCR = new AcctSeed__Billing_Cash_Receipt__c();
        objBCR.AcctSeed__Billing__c = billings[0].id;
        objBCR.AcctSeed__Cash_Receipt__c = objCashReceipt.id;
        objBCR.AcctSeed__Applied_Amount__c = objCashReceipt.AcctSeed__Amount__c;
        insert objBCR;
        }
        }
        }

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
Ravi Dutt SharmaRavi Dutt Sharma
Hi Deepali,

Having DML statments inside a for loop is highly discouraged - line 35 and 40 in your code.