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
tcsellstcsells 

Inserting Parent Child objects with DML

Hi,

I have a custom object, MIS_Job__c that is a child relationship for Account. The APEX documentation says the following should work:

 

insert new Account(Name='Joe Smit', MIS_Jobs__r = new MIS_Job__c( Category__c='XXX', Status__c='ZZZ'));

 

However, when I do this, I get a compile error "Field is not writeable: MIS_Jobs__r"

All I am trying to do is create parent/child objects for a test case. Thank you for any help you can provide.

Andy BoettcherAndy Boettcher

You need to create your parent object first, then you can insert the Id in your child object.

 

If by chance your code snip will work inline like that, it's "MIS_Jobs__c", not "__r".

 

The "__r" is for navigating relationships in a SOQL query.

 

-Andy

t.sellst.sells

Thanks for your response, Andy.

 

I am wondering if the problem is related to my test.  The code below will generate a unique Account ID, but my routine has a debug line that tests to see if MIS_Jobs is null, and it is for every case.  I know that I am doing something wrong, but I don't know what it is.  Thanks for any additional insights you can provide.

 

@isTest
public class TESTDeployClass2 {
    static testMethod void runDEPLOYTestCases(){
        List<sObject> acctList = new List<sObject>();
        
        for (Integer i = 0, j = 0; i < 10; i++) {
            Account myTestAcct=new Account(Name='Joe Smith'+i);             
            insert myTestAcct;
            insert new MIS_Job__c(Account__c=myTestAcct.ID, Category__c='XXX',  Status__c='YYY');
            
            acctList.add(myTestAcct);
        } 
        TESTDeploy.ViewJobs(acctList);
    }
}

 

Andy BoettcherAndy Boettcher

First place I'd check are your APEX logs after the test runs - are the DML statements returning any sort of error?

 

-Andy

t.sellst.sells

I have been checking the logs, but no apparent errors.  The output for each child item simply states:

 

08:38:05:169 DML_BEGIN [9]|Op:Insert|Type:MIS_Job__c|Rows:1

08:38:05:207 DML_END [9]|

Andy BoettcherAndy Boettcher

In your test class, separate out the DML from the instantiation and debug it out to see what's exactly getting inserted.

 

MIS_Job__c mj = new MIS_Job__c(Account__c=myTestAcct.ID, Category__c='XXX',  Status__c='YYY');

System.Debug('\n\n\n*** - ' + mj);

 

insert mj;

t.sellst.sells

This is giving me the expected results with each account ID being unique:

 

09:06:39:000 USER_DEBUG *** - MIS_Job__c:{Account__c=001J0000009dRi6IAE, Category__c=XXX, Status__c=YYY}

 

Inside my routine that is being tested I have the following construct.  This code works when I use actual data (seeAllData=true).

 

sObject[] myjobs= acct.getsObjects('MIS_Jobs__r');

if (myjobs!= null){
    //Other code
else{
   System.Debug('jobs were NULL');
     }

 

Andy BoettcherAndy Boettcher

Hmmm - we may be getting to the point where you posting your class and test class may be the best way to resolution here...

 

-Andy

t.sellst.sells

I need to apologize for my "beginner's error" on this.  My calling program for my routine was passing info from a SOQL statement that included a sub-query on the child objects.  So my test case was not doing this.  Here is my final, working test code.

 

@isTest
public class TESTDeployClass2 {
    static testMethod void runDEPLOYTestCases(){
        
        for (Integer i = 0, j = 0; i < 10; i++) {

            Account myTestAcct=new Account(Name='Joe Smith'+i, Type='ZZZ');             
            insert myTestAcct;
 
            MIS_Job__c mj= new MIS_Job__c(Account__c=myTestAcct.ID, MISNumber__c = '000',Category__c='XXX',  Status__c='YYY');
            insert mj;

           // acctList.add(myTestAcct);
        } 

       String queryString = 'Select a.AccountNumber, a.Type, a.Name, a.Id, (Select MISNumber__c, Category__c, Status__c From MIS_Jobs__r) From Account a WHERE a.type = \'ZZZ\' ';
       List<sObject> AcctList = Database.query(queryString);

       TESTDeploy.ViewJobs(acctList);
    }
}

 

Thank you for your help with this. Your guidence did lead me to this solution - and I am grateful :)

 

- Todd

Andy BoettcherAndy Boettcher

AAAAHHHH - that makes sense!  Awesome!

 

-Andy