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
Olga Kim 5Olga Kim 5 

find a parent records and create a child records in a single trigger

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;
        }}}
Agustin BAgustin B
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.
SSP AdminSSP Admin
Hi Olga,
It looks like our team of experts can help you resolve this ticket. We have Salesforce global help-desk support and you can log a case and our Customer Success Agents will help you solve this issue. You can also speak to them on live chat. Click on the below link to contact our help-desk. Trust me it is a support service that we are offering for free!

https://jbshelpdesk.secure.force.com

 Thanks,
Jarvis SFDC team