+ Start a Discussion

Using sObjectType.newSObject on a Page causing errors

If I use the code below, and click the button, instead of getting the expected error (Error: You must enter a value) and stopping the process, I instead get this error:


System.FinalException: SObject row does not allow errors


But if I comment out the line in bold in the controller, and use the more typical syntax on the line below (commented out), it works just fine. Is that expected behavior, or should I be doing something different to make it work?


When I use the first version, and I do enter a name, it does save the Campaign correctly though, which suggests the object is being create properly?


<apex:page controller="campTest">
<apex:form >
<apex:pageBlockSection columns="1">
<apex:commandButton action="{!CreateandAddCampaign}" value="Test"/>
<apex:inputField required="true" value="{!newCampaign.Name}"/>


public with sharing class campTest {

public Campaign newCampaign;

public Campaign getnewCampaign () {
if (newCampaign == null) {
newCampaign = (Campaign)Campaign.sObjectType.newSObject(null, true);
//newCampaign = new Campaign();
newCampaign.IsActive = TRUE;
return newCampaign;

public PageReference CreateandAddCampaign () {
insert newCampaign;
return null;



Rahul SharmaRahul Sharma

Yeah, Seems issue with loadDefaults parameter. When it is kept as false, it works otherwise simply fails.

If you want to make it working with use or loadDefaults = true, then you need to use javascript validation.