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
Alexander KrykunAlexander Krykun 

Field is not writeable: Paren-child

I have many to many relationship  between 3 objects:

User-added image

I want to insert list of sumarry(child) to parent object(accuunt)
Account acc = [Select ID, name,(SELECT id, Name FROM sumarry__r) from Account where name = 'apple'];

sumarry__c one = new sumarry__c();
sumarry__c two = new sumarry__c();
one.name = 'one';
two.name = 'two';
lists.add(one);
lists.add(two);
how to make it properly.
this operation acc.sumarry__r=lists cause 
"Field is not writeable: sumarry__r" 
or acc.sumarry__r.add(one) cause 
'System.QueryException: List has no rows for assignment to SObject'

 

Gadilkar VikasGadilkar Vikas
In this case you have to assign queried paent Id to the child.parent relationship field (AccountId) instead of assigning list of summary__c records to account object. please see below example:

Account acc = [Select ID, name from Account where name = 'apple'];
sumarry__c one = new sumarry__c();
one.name = 'one';
one.Account = acc.Id;

sumarry__c two = new sumarry__c();
two.name = 'two';
two.Id = acc.Id;
lists.add(one);
lists.add(two);

Insert lists;

hope this helps!!
Alexander KrykunAlexander Krykun
So, this example demonstrates access to parent object through child object.
Are there any ways to get the same result via like  mirror reflection through the parent object?