1

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?

1

Well, the following works for me ... I couldn't reproduce your error exactly but could definitely get the default values to appear on the page and the Validation Rule error to show if an invalid value was entered.

Apex controller

public class FoowDefaultController {
    public Foo__c f {get; set;}
    public FoowDefaultController(ApexPages.StandardController ctlr) {
        this.f =  ctlr.getId() == null
            ? (Foo__c) Foo__c.sobjectType.newSObject(null,true)
            : (Foo__c) ctlr.getRecord();
    }
    public PageReference save() {
        try {upsert this.f;}
        catch (DmlException e) {
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage()));
            return null;
        }
        return new PageReference('/'+this.f.id);
    }
}

Visualforce page

<apex:page standardController="Foo__c" extensions="FoowDefaultController">
    <apex:pageMessages/>
    <apex:form>
        <apex:commandButton action="{!save}" value="save"/>
        <apex:inputField value="{!f.someDefaultedField__c}"/>
        <apex:inputField value="{!f.someVrField__c}"/>
    </apex:form>
</apex:page>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.