I have sObject1 called sObject1__c which is a parent(master-detail) to many sObjects2 each called sObject2__c.

So in my trigger I have an empty list of sObject2 related to sObject1:

sObject1__c.sObject2s__r = ();

So then I query for sObject2 and iterate over the returned list of results assigning its values to sObject1 like this:


The code builds however the list sObject1__c.sObject2s__r remains empty. System.debug(sObject2__c) shows me that the object holds a value. Nonetheless that object doesn't get assigned to the corresponding list in sObject1.


Here's some code as requested:

List<Interview_Result__c> listOfTestResults = [
            FROM Test_Result__c
            WHERE Test_Process__c IN : mapOfTP.keySet()
for (Test_Process__c tprocess : mapOfTP.values()) {
    for (Test_Result__c tresult : listOfTestResults) {
        if (tprocess.Id == tresult.Test_Process__c) {

Please note that the tprocess.Test_Results__r is empty before I start adding to it.

  • please post your complete code
    – Himanshu
    Commented Mar 25, 2016 at 18:44
  • @Himanshu - I did add some code. I hope that makes things more clear for you.
    – Arthlete
    Commented Mar 25, 2016 at 19:24

1 Answer 1


I don't believe this construction is valid as child relationships can't be dynamically added to sobjects unless the original query included the child sObject

For example:

Account[] aList = [select id, name from Account limit 1];
for (Account a: aList) {
    a.contacts.add(new Contact (firstname = 'foo', lastname = 'bar', email= '[email protected]', accountId = a.id));
    system.debug(logginglevel.INFO,a.name + ' has contacts: ' + a.contacts);


someAcctName has contacts: ()

You can populate via Apex parent relationships as in the following:

Contact c = new Contact(firstname = 'foo', lastname = 'bar', email ='[email protected]',
                                           account = new Account (name = 'someacctname'));
system.debug(logginglevel.INFO,'contact account name='+c.account.name);
  • So is there a work around my problem? How can I resolve my issue?
    – Arthlete
    Commented Mar 25, 2016 at 20:01
  • 1
    @arthlete just create an independent list of Test_Result__c and be sure to populate the lookup field to Test_Process__c; then you can do dml on the testResults list
    – cropredy
    Commented Mar 25, 2016 at 20:39
  • Thanks. My problem is that the Test_Process__c is captured by the trigger having an empty lookup field as you referred to it.
    – Arthlete
    Commented Mar 25, 2016 at 20:46
  • 1
    @arthlete ah, since Test_process__c is the parent, in your trigger, simply requery [select ..., (select id, ... from Trigger_results__r) from Trigger_process__c where id in :trigger.new.keyset()] as the child relationships aren't implicitly in trigger scope. Or, I may be misunderstanding your issue altogether :-(
    – cropredy
    Commented Mar 25, 2016 at 21:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .