function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BritishBoyinDCBritishBoyinDC 

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:pageBlock>
<apex:pageBlockSection columns="1">
<apex:commandButton action="{!CreateandAddCampaign}" value="Test"/>
<apex:inputField required="true" value="{!newCampaign.Name}"/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
 
</apex:page>

 

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.