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
mr209204mr209204 

Saving

What I'm trying to do is save information from a form. Everything I have done is based off examples from SF Docs like the Cookbook so forgive me if things look a little haphazard. This is the code that I have so far. I know that I have to Include <apex:commandButton action="{!save}"> somewhere in there and that I have to create a method for it. This is where I am stuck. I don't quite understand the examples given to me in the cookbook or the visual force documentation. Here is the apex that I have so far. How do I go about saving all this information once entered?



<apex:page Controller="testController" tabStyle="Contact">
<apex:sectionHeader title="Create new Contact" />

<apex:form >
<apex:pageBlock title="Contact" id="thePageBlock" mode="edit">
<apex:pageMessages />
<apex:pageBlockButtons >

</apex:pageBlockButtons>

<apex:actionRegion >
<apex:pageBlockSection title="Contact Information" columns="1">
<apex:inputField value="{!contact.firstName}"/>
<apex:inputField value="{!contact.lastName}"/>
<apex:inputField value="{!contact.MailingStreet}"/>
<apex:inputField value="{!contact.MailingCity}"/>
<apex:inputField value="{!contact.MailingState}"/>
<apex:inputField value="{!contact.MailingPostalCode}"/>
<apex:inputField value="{!contact.HomePhone}" />
<apex:inputField value="{!contact.MobilePhone}" />
<apex:inputField value="{!contact.Fax}" />
<apex:pageBlockSectionItem >
<apex:outputLabel value="Add Company"/>
<apex:outputPanel >
<apex:inputField value="{!contact.Add_Company__c}">
<apex:actionSupport event="onchange" rerender="thePageBlock"status="status2"/>
</apex:inputField>
<apex:actionStatus startText="applying value..." id="status2"/>
</apex:outputPanel>
</apex:pageBlockSectionItem>
</apex:pageBlockSection>
</apex:actionRegion>

<apex:pageBlockSection title="Company Information" columns="1"
rendered="{!contact.Add_Company__c == true}">

<apex:inputField value="{!account.name}" />
<apex:inputField value="{!account.BillingStreet}" />
<apex:inputField value="{!account.BillingCity}" />
<apex:inputField value="{!account.BillingState}" />
<apex:inputField value="{!account.BillingPostalCode}" />
<apex:inputField value="{!account.Phone}" />
<apex:inputField value="{!account.Fax}" />
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>

</apex:page>
So what would I have to add on the controller side?



Message Edited by mr209204 on 07-21-2008 02:23 PM
JeriMorrisJeriMorris
I'm new at this too, so I know how you feel....

Your controller has to have a save() method. Assuming you're working on the example where you create an Account, Opportunity, and Contact in one fell swoop, it should look something like this:

public PageReference save() {

// Create the account
insert account;

// Create the Contact
contact.accountId = account.id;
insert contact;

// Create the Opportunity
opp.accountId = account.id;
insert opp;

// Assign the Contact the given OpportunityContactRole
role.opportunityId = opp.id;
role.contactId = contact.id;
insert role;

// Display the results page
PageReference pg = Page.AccountOppContact_Results;
pg.setRedirect(true);
pg.getParameters().put('oppid', opp.id);
return pg;
}

Note that the signature of the save method (or any other method that you invoke from a VisualForce page) can't include any arguments. When you invoke the save method from your VisualForce code, you don't even use parentheses -- that is, you invoke it with {!save} and NOT {!save()}.  (Figuring that out cost me a few hours....)

I hope this helps!

Jeri