Skip to main content
Hi I am new in Apex. Please help me resolve this problem.

I have a requirement to create a child record (on Billing Line object) if parent records (on Billing object) exist I need to attach the child record to the parent but if Parent record doesn't exist I need to create a parent and a child records. 

This trigger is supposed to fire when I check a checkbox on a TEST object. 

For some reason, my code can't find parent records and can't create one.   

public class testBillingLineCreate {

    public static void createBillingLine (list<TEST__c> TestList, map<id, TEST__c> oldMap)

    {

     list<AcctSeed__Billing_Line__c> billingLineList=new List<AcctSeed__Billing_Line__c>();      

     list<AcctSeed__Billing__c> billingList= new list<AcctSeed__Billing__c>();

        

           for(TEST__c objtest:TestList)

        {

        if(objtest.Billed__c==true && oldMap.get(objtest.Id).Billed__c==false)

            

        {

            for(AcctSeed__Billing__c billings:[select id, name, AcctSeed__Customer__r.id, advAcctSeed__Matter__r.id from AcctSeed__Billing__c where AcctSeed__Status__c='Approved']){

            

                if(billings.AcctSeed__Customer__r.id==objtest.Account__r.id && billings.advAcctSeed__Matter__r.id==objtest.Matter__r.id){

                

            AcctSeed__Billing_Line__c bill=new AcctSeed__Billing_Line__c();

           bill.AcctSeed__Billing__r.id=billings.Id;

            bill.AcctSeed__Project__c=objtest.Project__c;

            bill.AcctSeed__Hours_Units__c=objtest.time__c;                  

            bill.AcctSeed__Rate__c=objtest.Rate__c; 

          

            billingLineList.add(bill);

        }

            

        

        

        

         else {

              AcctSeed__Billing__c billings2=new AcctSeed__Billing__c();

              billings2.AcctSeed__Customer__c= objtest.Account__c;

               billings2.advAcctSeed__Matter__c=objtest.matter__c;

            billingList.add(billings2);

            

               AcctSeed__Billing_Line__c bill1=new AcctSeed__Billing_Line__c();

           bill1.AcctSeed__Billing__c=billings2.Id;

            bill1.AcctSeed__Project__c=objtest.Project__c;

            bill1.AcctSeed__Hours_Units__c=objtest.time__c;                  

            bill1.AcctSeed__Rate__c=objtest.Rate__c; 

          

            billingLineList.add(bill1);

            

            }

    }

           

        }

        insert billingList;

        insert billingLineList;

        }}}
1 Antwort
  1. 8. Mai 2020, 21:07
    Hi Olga, if nothing is happening it may be because you are not getting anything here:

     for(AcctSeed__Billing__c billings:[select id, name, AcctSeed__Customer__r.id, advAcctSeed__Matter__r.id from AcctSeed__Billing__c where AcctSeed__Status__c='Approved']){

    Create a test class, test data for that test class (or use seeAllData=true but it is not the best practice) and start debugging where your code is failling.

    If it helps you please mark this answer as correct, it may help others.
0/9000