You need to sign in to do that
Don't have an account?
tcsells
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.
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
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.
First place I'd check are your APEX logs after the test runs - are the DML statements returning any sort of error?
-Andy
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]|
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;
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).
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
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.
Thank you for your help with this. Your guidence did lead me to this solution - and I am grateful :)
- Todd
AAAAHHHH - that makes sense! Awesome!
-Andy