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
Jamie FellrathJamie Fellrath 

How do I add a Contact and Account on one Apex Site page

Hi all,

Just getting started with Apex and Visualforce pages and I hope someone can help me out here.  We have account reps who need me to create a public-facing contact entry page that takes a parameter. The parameter is just for tracking purposes and is written to a custom field called Lead_Channel__c. (So the URL is something like http://ourorg.force.com/?Lead_Channel=<eventname> and the parameter is written to Lead_Channel__c).

The idea is to get the person's contact information - First/Last name, email, phone, title... and Company Name.

The trick is that Company Name - it's an Account field, not a Contact field. I have the page working to create the Contact right now, but they want it to create a new Account and Contact. 

Here's my VF page code, after my having added a field for the Contact.Account.Name (the working version simply omits that line):
<apex:page controller="ContactCreateController" showHeader="false" sidebar="false">
	<apex:sectionHeader subtitle="Create a Contact" />
	<apex:form style="margin-left: 210px; margin-top: 50px; margin-bottom: 100px; margin-right: 210px;">
		<apex:pageMessages />
		<apex:pageBlock title="New Contact Entry">
			<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:inputField value="{! Contact.Phone }" />
				<apex:inputField value="{! Contact.Title }" />
				<apex:inputField value="{! Contact.Account.Name }" />
			</apex:pageBlockSection>
		</apex:pageBlock>
	</apex:form>
</apex:page>
And here's the Custom Controller code - without changes to insert the Account based on the name, first.
 
public with sharing class ContactCreateController {
    private String leadChannel {get;set;}
    // the contact record you are adding values to
    public Contact contact {
        get {
            if (contact == null)
                contact = new Contact();
            return contact;
        }
        set;
    }
    
    public ContactCreateController() {
        /* Extract lead channel from URL and assign to variable */
        leadChannel  = ApexPages.currentPage().getParameters().get('Lead_Channel');
    }
    
    // save button is clicked
    public PageReference save() {
        try {
            contact.Lead_Channel__c  = leadChannel; /*Save lead channel to contact */
            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.ContactCreateThankyou;
    }
}

The ContactCreateThankYou page simple displays the new data and then any other additions the reps want to add in.

Can anyone help me to modify my code to get that Account added? 

Thanks,

Jamie
 

Best Answer chosen by Jamie Fellrath
Harish RamachandruniHarish Ramachandruni
Hi ,

Type Site in quick Search select site lable in list of site table list  .

Click on public settings .

edit profile give read and create permetion of account and contact .

enabel page and controller to that profile . 

add site defalt page of your page .



Regards ,
Harish.R 

All Answers

Harish RamachandruniHarish Ramachandruni
Hi ,

Type Site in quick Search select site lable in list of site table list  .

Click on public settings .

edit profile give read and create permetion of account and contact .

enabel page and controller to that profile . 

add site defalt page of your page .



Regards ,
Harish.R 
This was selected as the best answer
Jamie FellrathJamie Fellrath
Thanks Harish. 

That got some of it set up, but the actual code functionality of adding the account (based on what I have in the first post) still eludes me.

Jamie
Jamie FellrathJamie Fellrath
I got it - after making Harish's suggested changes/additions, I added some lines to my code: 
 
public with sharing class ContactCreateController {
    private String leadChannel {get;set;}
    // the Account and Contact records you are adding values to

    public Account account {
        get {
            if (account == null)
                account = new Account();
            return account;
        }
        set;
    }
    
    public Contact contact {
        get {
            if (contact == null)
                contact = new Contact();
            return contact;
        }
        set;
    }
    
    public ContactCreateController() {
        /* Extract lead channel from URL and assign to variable */
        leadChannel  = ApexPages.currentPage().getParameters().get('Lead_Channel');
    }
    
    // save button is clicked
    public PageReference save() {
        try {
            Insert account;
            contact.Lead_Channel__c  = leadChannel; /*Save lead channel to contact */
            contact.AccountId = account.Id;
            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.ContactCreateThankyou;
    }
}

And this works - creates the Account and attaches it to the Contact.  Thank you!