1
votes

I have recently activated and configured Person Accounts in our salesforce org. We will be using these within the service console and also integrating Postcode Anywhere for address lookups. To integrate PA in the console, it has to be done via a visualforce page as there is no sidebar to load the app. To resolve this, I have created a Visualforce page that covers all account record types, including Person Accounts and have overridden the 'new' and 'edit' buttons to call the VF page.

So far so good. Now, when editing accounts, everything works currently, all fields show and PA works fine. BUT, when creating a NEW Person Account, the Salutation, First Name and Last Name field so not show on the page and so the record cannot be saved (as Last Name is required to create an account).

Having spoken to Salesforce about this (as this is quite a fundamental issue), they claim the those 3 fields have no API field definitions and so cannot be called via a VF page. That's obviously no correct as they work fine in edit mode (having created a person account using the standard page) and they are also listed in the WSDL file.

Having Google'd a LOT, I believe the problem is there because the isPersonAccount boolean field is not set at the point of creating the account (in VF) and therefore the page does not allow the name fields to render.

So on to my question (I've not had much experience with VF or controllers), is it possible to somehow set the isPersonAccount field to true using a controller extension or something similar, when creating a person account via VF. Or can the page be somehow saved before presenting it to the user so the field gets set and the name fields then render?

Any help with this would be massively appreciated as Salesforce aren't being exactly helpful and I can't be the only person in the world that needs to create a person account via a visualforce page?

2

2 Answers

2
votes

You can certainly use a controller extension to resolve the issue. The idea would be to provide the VisualForce page with whatever additional fields you need via apex:inputText. Then take the fields' values and write them to the object upon save.

As you now have no need to rely on the standard controller to render the field, you would not need to explicitly set the isPersonAccount field.

3
votes

Resolved as follows:

Controller code (partial):

Account acct; 
public String salutation {get; set;}
public String fname{get; set;}
public String lname{get; set;}

public PageReference save() {
    try {
        // check for person account record type
        recType = [select id,name,sobjectType,ispersontype from recordType where ispersontype=true and sobjectType='account' limit 1];
        if(acct.recordtypeid == recType.id) {
            acct.Salutation=salutation;
            acct.FirstName=fname;
            acct.LastName=lname;
            insert(acct);                    
            PageReference newPage = New PageReference('/'+acct.id);
            newPage.setRedirect(true);
            return newPage;
        }
        else {
            insert(acct);    
            PageReference newPage = New PageReference('/'+acct.id);
            newPage.setRedirect(true);
            return newPage;
        }
    }
    catch(System.DMLException e) {
            ApexPages.addMessages(e);
             return null;
    }  
    return null;
}

Visualforce code (partial):

<apex:pageblocksectionitem >
    <apex:outputlabel value="First Name"/>
    <apex:outputpanel >
        <apex:inputText value="{!salutation}" id="personSalutation"/>
        <apex:inputText value="{!fname}" id="personFname"/>
     </apex:outputpanel>
</apex:pageblocksectionitem>
<apex:pageBlockSectionItem>
    <apex:outputLabel value="Last Name" for="personLname"></apex:outputLabel>
    <apex:inputText value="{!lname}" id="personLname"/>
</apex:pageBlockSectionItem>