Field is not writable: Getting this error message on a Master - Detail relationship field?

4:15 PM

Let's say that you have two objects

Object_A__c (Master)
Object_B__c (Detail)

Object_B__c has a field:
fieldobja__c (Type: Master-Detail)

Now, you get the error "Field is not writable" whenever you try to assign a value to the field "fieldobja__c" in either an Apex Class or a Trigger. Why??

This is because you can assign a value to this field only when you do an INSERT.
When you do an UPDATE, you CANNOT assign a value to this field.

In other words, you can assign a value only once.

Assign a value only when the field has a value of NULL.

And this restriction does make sense, as this is a Master-Detail relationship.