+ Start a Discussion
ocsden1ocsden1 

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!
Message Edited by ocsden1 on 07-14-2009 09:39 AM
Ron HessRon Hess

I think you are close, you also need to set    isPersonAccount=true

 

in your new Account() statement

 

 

ocsden1ocsden1
Thanks for the response Ron! I've updated the new Account() statement as follows:

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!
WilmerWilmer

Hi, I´m having the same problem. Does anybody know how to fix it? Is there any workaround?

 

Regards,

 

 

Wilmer

rchanderrchander

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:

 

 public Contact contact;

 

public Contact getContact(){

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;
    }

 

sfdcfoxsfdcfox

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;

 

 

 

rchanderrchander

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.

 

donrdonr

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!

 

-------------------------------------------------------------------------------

 

 

/*
* 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='01230000000ALNQAA4', firstname='Sammy', lastname='Snead', phone='907-111-2222');
        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;
    }
}

 

vanessenvanessen

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.

izayizay

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: 

 

<apex:pageBlockSectionItem rendered="{!isPerson}">
    <apex:outputLabel>First Name</apex:outputLabel>
    <apex:pageBlockSectionItem rendered="{!isPerson}">
        <apex:inputField id="Salutation" value="{!contact.Salutation}" rendered="{!isPerson}"/>
        <apex:inputField id="FirstName" value="{!contact.FirstName}" style="width:100px" rendered="{!isPerson}"/>
    </apex:pageBlockSectionItem>
</apex:pageBlockSectionItem>

 

A Dev in NZA Dev in NZ

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?

 

vanessenvanessen

try to use isPersonAccount