Skip to main content
Olga Kim (Olga Kim) が「#Apex」で質問
Dear Salesforce Professional

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 (Billing Line) to the parent (Billing) 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__Customer__c]: [AcctSeed__Customer__c] Class.testBillingLineCreate.createBillingLine: line 48, 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__c, advAcctSeed__Matter__c from AcctSeed__Billing__c where AcctSeed__Status__c='Approved']){

          if(billings.AcctSeed__Customer__c==objtest.Account__c && billings.advAcctSeed__Matter__c==objtest.Matter__c){

            AcctSeed__Billing_Line__c bill=new AcctSeed__Billing_Line__c();

           bill.AcctSeed__Billing__c=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;

            billings2.AcctSeed__Status__c='Approved';

             billings2.AcctSeed__Date__c=date.today();

             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;

        }}}
5 件の回答
  1. 2020年5月11日 23:57
    @abhishek - I love a canned response.  If we hope to guide the new programmers, a little explaination will always help rather than just do it this way.

    @Olga

    Alright, to answer the questions,lets review the code and point some things out.

    1.  You have a SOQL query inside a FOR loop.  This is an issue for bulkification.  There are plenty of posts regarding that issue and how to overcome it.

    2.  Why are you getting the insert error?  I would guess to say that that the field is not set in the TEST__c object that you are using as a reference point.  If you wanted to find out if this was the cause, throw a debug in the code to test for the value in the field.  Or nest the creation of the record in an IF statement to test that the values exist in the TEST__c record.

    3.  You will have future issues.  Which is why @abhishek posted the associated links.   Even if you get past the error you are experiencing, you will have issues on the insert of the billingLineList.  It may not error, but will be an issue.  Why?

    Because of this line:

    bill1.AcctSeed__Billing__c=billings2.Id;

    The billings2 record has not been inserted.  The Id for the record will not exist prior to being inserted.  Therefore you will be setting the AcctSeed__Billing__c to null.  And so will not have the relationship you are trying to establish.

    You could implement a simple piece of code below to prove that Ids do not exist until inserted.

    Lead beforeLead = new Lead(LastName='Sample',

    FirstName ='A',

    Company='ACME',

    Status = 'Open - Not Contacted',

    Email = 'sample@acme.acme');

    System.assertEquals(null,beforeLead.Id);

    insert beforeLead;

    System.assertNotEquals(null,beforeLead.Id);

    Hopefully that is clear. 

    I would now recommend that you check the links that @abhishek posted and adjust your code.  Keep in mind points 1 & 2 above.

    Regards

    Andrew

     
0/9000