I am trying to modify a list of sobjects that I get from a subquery but it appears that adds to the list are simply silently ignored. sample code:
Account acc = [select ID, (select ID from Contacts limit 1) from Account limit 1]; Integer consize = acc.contacts.size(); acc.contacts.add(new Contact(lastname='test')); system.assertequals(consize+1,acc.contacts.size());
Is this expected behaviour? I might be misremembering but I could swear I've done this in the past.
UPDATE: Setting acc.contacts directly will also not work as you get the following error:
Field is not writeable: Contacts
If you assign acc.contacts to a list you can then work with the list as normal but this is not ideal for my use case as I have a function that I want to return a list of sobjects with 1-n subqueries.
UPDATE 2: It appears that you can modify the properties of the sobjects in the list, just not the list itself.
Account acc = [select ID, (select ID from Contacts limit 1) from Account limit 1]; list<Contact> con1 = acc.contacts; con1.get(0).lastname = 'TEST'; system.assertequals('TEST',acc.contacts.get(0).lastname); list<Contact> con2 = acc.contacts.clone(); con1.get(0).lastname = 'TEST2'; system.assertequals('TEST2',acc.contacts.get(0).lastname);