Master-Detail: Field is not writable error on Apex

I came upon this error when I assigning the custom object to the master standard object.

Invoice__c inv = new Invoice__c();
inv.Account__c = accountId;
insert inv;

Then I get this error.

Field is not writable : Invoice__c.Account__c

The fix was actually easy. I just rewrote it to assign the master id on instantiating the child object.

Invoice__c inv = new Invoice__c(Account__c = accountId);
insert inv;

Hope somebody finds this useful.

Post Author: lopau

2 thoughts on “Master-Detail: Field is not writable error on Apex


    (September 8, 2016 - 3:09 am)

    This solution is very helpful and easy also. Thanks 🙂


    (September 12, 2016 - 3:06 am)

    Glad you found it helpful.

Leave a Reply

Your email address will not be published. Required fields are marked *