You need to sign in to do that
Don't have an account?

Creating a Person Account with Visualforce and a Custom Controller
I'm having a problem with the firstname and lastname inputField on my visualforce page designed to create a new Person Account. The input field for the first and last name fields are showing up as non-editable. I have read several posts and am attempting to set the recordTypeID in the controller, but as you will see in my simple example it does not appear to be setting properly. Any help with the following greatly appreciated.
Page:
<apex:page controller="newPersonAccountController" tabStyle="Contact">
<apex:sectionHeader title="New Person Account Header"/>
<apex:form >
<apex:pageBlock title="New Person Account Block">
<apex:facet name="footer">
<apex:commandButton action="{!save}" value="Save" styleClass="btn"/>
</apex:facet>
<apex:pageBlockSection title="Person Account Section">
<apex:panelGrid columns="1">
<apex:inputField value="{!PersonAccount.phone}"/>
<apex:inputField value="{!PersonAccount.firstname}"/>
<apex:inputField value="{!PersonAccount.lastname}"/>
<apex:outputLabel value="{!PersonAccount.RecordTypeID}"/>
<apex:outputLabel value="{!PersonAccount.isPersonAccount}"/>
</apex:panelGrid>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller:
/*
* This class is the controller behind the New PersonAccount
* wizard.
*/
public class newPersonAccountController {
// Variables
account PersonAccount;
// Get methods.
public account getPersonAccount() {
if(PersonAccount == null) PersonAccount = new account(RecordTypeId='012A000000006JQ', firstname='Sammy', lastname='Snead');
return PersonAccount;
}
// This method performs the save for both objects, and
// then navigates the user to the detail page.
public PageReference save() {
// Create the account.
insert PersonAccount;
// Finally, send the user to the detail page
PageReference providerPage = new PageReference('/' + PersonAccount.id);
providerPage.setRedirect(true);
return providerPage;
}
}
Results:
The vf page displays with phone as an editable field and firstname and lastname as non-editable fields. The recordtypeID is displayed, the isPersonAccount field shows as false. The record DOES save correctly and creates the person account with the correct record type.
Any thoughts on how to get the firstname and lastname fields to be editable fields, or any relavent sample code greatly appreciated.
Thanks!
I think you are close, you also need to set isPersonAccount=true
in your new Account() statement
if(PersonAccount == null) PersonAccount = new account(RecordTypeId='012A000000006JQ', isPersonAccount='true', firstname='Sammy', lastname='Snead');
And received the following error on save:
Error: Compile Error: Field is not writeable: Account.IsPersonAccount at line 14 column 114
Any other thoughts?
Thanks!
Hi, I´m having the same problem. Does anybody know how to fix it? Is there any workaround?
Regards,
Wilmer
Kindly use the following:
in visualforcepage:
<apex:outputLabel id="id" value="First Name" style="padding: 2.5em; font-size:8pt;align:right;font-weight: bold;"/>
<apex:inputField id="Salutation" value="{!contact.Salutation}"/>
<apex:inputField id="FirstName" value="{!contact.FirstName}" style="width:100px"/><apex:inputField id="LastName" value="{!contact.LastName}" />
In the controller:
if (contact==null){contact =
new Contact();}
return contact;}
public account getPersonAccount() {
if(PersonAccount == null) PersonAccount = new account(LastName='Snead');
return PersonAccount;
}
public PageReference save() {
// Create the account.
PersonAccount.Salutation= contact.Salutation;
PersonAccount.FirstName = contact.FirstName;
PersonAccount.LastName = contact.LastName
insert PersonAccount;
// Finally, send the user to the detail page
PageReference providerPage = new PageReference('/' + PersonAccount.id);
providerPage.setRedirect(true);
return providerPage;
}
You can't (shouldn't) update the contact of a person account directly. Instead, specify a person account record type and assign that to the RecordTypeId field. Then use the appropriate "Person*" fields, or "*__pc" fields for custom fields.
RecordType recType = [select id from recordType where ispersonaccount=true and sobjectType='account' limit 1]; account a = new account(recordtypeid=recType.id, personfirstname='John', personlastname='Doe',personemail='jdoe@salesforce.com'); insert a;
Yeah! getting the record type seems to be a good practice.
However when you create a account with the the last name it automatically takes it as a personaccount
and the record type is of a person account.
I'm a noob at writing visualforce pages.. I'm attmepting to write a page and controller to add new PersonAccounts, I grabbed the code posted in this thread. It generates the following error;
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Last Name]: [Last Name]
I'm out of ideas.. Any help would be appreciated!
-------------------------------------------------------------------------------
Thanks for the tip on isPersonAccount. In fact if i do not retrieve this field,it doesn't allow me to use any of the person account field on my vf page.
I know that this thread is kinda old but I was having the same issue and found these information useful. I just wanted to add something else for future reference.
To display the Salutation and First Name inputfields on the visualforce page:
Has anyone found an answer to this?
My only approach was to change the "FirstName" and "LastName" inputFields and make them outputLabels with inputText inside a pageBlockSectionItem.
I also left the RecordTypeId field off the page it was also getting in the way of rendering a "New" Person Account Edit Page.
Any other, more solid, solutions?
try to use isPersonAccount