I have a Visualforce page that is backed by a custom controller. The custom controller has a public property of type Event
(called ThisEvent
) that the Visualforce page uses to bind to several input fields.
The constructor of this custom controller sets the value of ThisEvent
by first calling into getRecord()
on the standard controller it extends. If the ID of this record is null, then it sets the value of ThisEvent
to the result of (Event)Schema.getGlobalDescribe().get('Event').newSObject(null, true)
. This is done so that any default values that have been set on the fields on this object are correctly set.
In essence, this looks like:
public Event thisEvent { get; set; }
public EventWithCallReport(ApexPages.StandardController controller) {
thisEvent = (Event)baseController.getRecord();
if(thisEvent.Id == null){
thisEvent = (Event)Schema.getGlobalDescribe().get('Event').newSObject(null, true);
}
}
This works fine in the sense that the default values are correctly set when rendering the create page.
However- once the user clicks save, if there are any validation errors on the submitted form, the user will see a white error screen with the message SObject row does not allow errors
. This only happens when creating a record. If I comment out the thisEvent = (Event)Schema.getGlobalDescribe().get('Event').newSObject(null, true);
line, validation works as expected.
So, how can I get both validation to work as expected, and for default values to be set when creating a record?