Using VisualForce & APEX to submit a new Contact record. Generating a custom form and trying to save the records as a new 'Contact' on my SF instance.
Once I click on 'Save' button below error is generated
VisualForce Page code
<apex:page Controller="ContactController" >
<apex:form >
<apex:pageBlock title="New Contact Form">
<apex:pageBlockSection columns="1">
<apex:inputField value="{!c.FirstName}"/>
<apex:inputField value="{!c.LastName}"/>
<apex:inputField value="{!c.Email}"/>
<apex:inputField value="{!c.Birthdate}"/>
</apex:pageBlockSection>
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockTable value="{!samepage}" var="c">
<apex:column headerValue="First Name">
<apex:outputField value="{!c.Firstname}"/>
</apex:column>
<apex:column headerValue="Last Name">
<apex:outputField value="{!c.Lastname}"/>
</apex:column>
<apex:column headerValue="Birthdate">
<apex:outputField value="{!c.Birthdate}"/>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form>
</apex:page>
APEX Controller Code
public with sharing class ContactController {
public Contact c { get; set; }
public List<Contact> samepage { get; set; }
//Constructor
public ContactController(){
c=new Contact();
}
public PageReference save() {
// Database.DMLOptions dml = new Database.DMLOptions(); // dml.DuplicateRuleHeader.AllowSave = true; Database.SaveResult sro = Database.insert(c, false);
if(sro.isSuccess()) {
// Operation was successful, so get the ID of the record that was processed
System.debug('Successfully inserted contact. Contact ID: ' + sro.getId());
}
else {
// Operation failed, so get all errors
for(Database.Error err : sro.getErrors()) {
System.debug('The following error has occurred.');
System.debug(err.getStatusCode() + ': ' + err.getMessage());
System.debug('Account fields that affected this error: ' + err.getFields());
}
}
return null;
} }