0

I am a beginner in Programming.

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 parent and 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.

I get this error message:

TriggerTestBillingLineCreate: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [AcctSeed__Billing__c]: [AcctSeed__Billing__c] Class.testBillingLineCreate.createBillingLine: line 38, column 1 Trigger.TriggerTestBillingLineCreate: line 5, column 1

Basically it is saying the required field is missing when I am trying to create a parent record. I don't understand why it is missing if I populate it there and in some cases why it needs to create a parent record if a parent exists in a database. It just needs to find it and link a child record to the parent.

I attached my code below. I appreciate any advice. Thank you for your help.

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
  • Welcome to SFSE. Could you please edit your post to share more detailed context on the failures you are observing? If you are receiving an error, include it verbatim with line number. If not, please identify exactly where in your code you are not getting expected behavior.
    – David Reed
    May 8 '20 at 22:42
0

I believe the issue is in your else statement. You are referencing the Billing before it is created. It does not have an Id until it is inserted:

} else {
    AcctSeed__Billing__c billings2=new AcctSeed__Billing__c();
    billings2.AcctSeed__Customer__c= objtest.Account__c;
    billings2.advAcctSeed__Matter__c=objtest.matter__c;
    insert billings2;                        <----- Missing insert
    //billingList.add(billings2); Don't add to list, already inserted.

    AcctSeed__Billing_Line__c bill1=new AcctSeed__Billing_Line__c();
    bill1.AcctSeed__Billing__c=billings2.Id; <----- Without an insert, ID does not exist and is missing.
    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);
}

The issue with this workaround is that it will reach the DML limit if you are running this for many records.

2
  • I inserted a parent record before creating a child but it still shows me an error message that required fields are missing. I think I need to change the whole logic of my helper class. Anyway, thank you for your advice.
    – olgaK
    May 14 '20 at 3:08
  • It still errors on the insert billingLineList line?
    – icebird76
    May 15 '20 at 4:04

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.