0

Given two objects Opportunity and Project where Project is a child of Opportunity (master object), I try to assign Project to Opportunity via a property:

Project__c updateProject = new Project__c();
updateProject.Opportunity__c = OpportunityId; 

When I do that, I get the error:

FATAL_ERROR System.SObjectException: Field is not writeable: Project__c.Opportunity__c

However, I am able to write to this property when I pass the value via a constructor:

Project__c updateProject = new Project__c(Opportunity__c = OpportunityId);

I'm guessing this is because a child record must be assigned to its parent record at the time of creation.

Can someone point me to documentation that explains this behavior?

3

1 Answer 1

0

While I cannot find an official documentation for this, the answer given here confirms my guess. (Thank you @venky.)

If it's master-detail then, by default, you can't 'reparent' the record, that is, write to the Employee__c field after the detail record has been created. You can, however, selecting 'Allow Reparenting' in the master-detail relationship definition to make it work.

The 'Allow Reparenting' is a flag that is set on a master-detail field. By default, this field is 'false'. Hence after creating an object, you cannot assign a new parent id.

enter image description here

You must log in to answer this question.

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