Create a New Record in Force.com Sites
April 27th, 2010
So someone asked me yesterday for some code to allow external users to create contact records in Salesforce.com. They needed a simple form where people could enter the details and once submitted receive a confirmation of what information was entered. Here’s what the final page that was developed looks like. You can try the code out on my developer site to see how it runs.
![]()
If this solution was meant to run inside the Salesforce.com UI, you would simply need a single Visualforce page that utilizes the standard controller for Contact. Once submitted, the standard controller would insert the new record and relocate the user to the display page for the new record.
However, since this is an external page we have to do a little more work. We need two Visualforce pages (one for the form and one for the confirmation page) and a custom controller to submit the new contact record and show the user the confirmation page. You’ll also need to set up a new Force.com Site and add the two new Visualforce pages to the site so they are accessible externally. You’ll also need to modify the Public Access Settings for the site to allow Read access to the Contact object.
Your code should look like the following:
Contact_Create Visualforce Page
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <apex:page controller="ContactCreateController">
<apex:sectionHeader title="Visualforce Example" subtitle="Create a Contact"/>
<apex:form >
<apex:pageMessages /> <!-- this is where the error messages will appear -->
<apex:pageBlock title="Contact Info">
<apex:pageBlockButtons >
<apex:commandButton action="{!save}" value="Save"/>
</apex:pageBlockButtons>
<apex:pageBlockSection showHeader="false" columns="2">
<apex:inputField value="{!contact.firstName}" />
<apex:inputField value="{!contact.lastName}" />
<apex:inputField value="{!contact.email}" />
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page> |
Contact_Create_Thankyou Visualforce Page
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <apex:page controller="ContactCreateController">
<apex:sectionHeader title="Visualforce Example" subtitle="Thank You"/>
<apex:form >
<apex:pageBlock title="Contact Info">
<apex:pageBlockSection showHeader="false" columns="2">
<apex:outputField value="{!contact.firstName}" />
<apex:outputField value="{!contact.lastName}" />
<apex:outputField value="{!contact.email}" />
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page> |
Apex Controller
public with sharing class ContactCreateController {
// the contact record you are adding values to
public Contact contact {
get {
if (contact == null)
contact = new Contact();
return contact;
}
set;
}
public ContactCreateController() {
// blank constructor
}
// save button is clicked
public PageReference save() {
try {
insert contact; // inserts the new record into the database
} catch (DMLException e) {
ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error creating new contact.'));
return null;
}
// if successfully inserted new contact, then displays the thank you page.
return Page.Contact_Create_Thankyou;
}
}Categories: Apex, Code Sample, Salesforce, Visualforce













Thanks for this! It cleared up the problem I was having immediately when I moved to using a public facing site. Now I am in the process of learning to create the test unit / classes for the respective custom controllers.. I wondered if you could include this as well? Or a quick tutorial? Also, I’ve purchased your book, the java google app engine – it’s great – thanks!
Josh, thanks for nice comments! I’ll try to do a unit test for these controllers next week some time. I’m leaving for a client’s site shortly.
This is great!
Can you offer the same sort of method for a custom object with a master detail record?
Thanks again!
@Lavi, on the Visualforce page you could display multiple rows for detail records to be created and when submitting the form you could create the master records and then iterate through the child records to be created, tag them with the master records ID and then insert them. HTH
Trying to do something similar to Lavi I think, but with custom objects.
It seems like apex:inputField only works with custom objects, because i get the following error message:
Error: Could not resolve the entity from value binding ‘{!Shipping_Method__c}’. inputField can only be used with SObject fields.
Any way to make something like this work for custom objects?
whoops – edit above to read
It seems like apex:inputField only works with *standard* objects
@Sara, the inputField will work with standard objects as well as custom ones. So if you are using the Account standard controller you can reference a field like {!account.BillingCity}. Essentially the same for custom objects as well. Try this page: http://blog.jeffdouglas.com/2010/04/07/easily-search-and-edit-records-with-visualforce/
ahh thanks – i was messing around with the object so much i eventually left it out!
Hi,
I have implimented the above into my Sites page and all work great with regards to redirecting the user to the thank you page and the saving of the record into SFDC.
However , I am only ever allowed to display one field on the sites page, all other field do not show.
I can show text that ive typed without and issue, but I can only show one , no matter what I do?
Any ideas at all would be great as Im at a loss now.
Thanks
Thanks , it really helpful!!