2

I have a VFP and an apex class. The page "overwrites" the "new" standard page to create an Account. Output/Input fields should be rendered according to the RecordType of the Account. Specifically middlename, firstname, etc.

Code of the VFP

<apex:form id="form1">
    <apex:pageBlock title="Add Accounts" mode="edit" id="pageblock1">
        <apex:pageBlockSection title="Account Information" columns="2" id="informationSection" Rendered="{! !isBusinessAccount}">
            <apex:inputField value="{!Account.FirstName}" id="firstname"/>
            <apex:outputField value="{!Account.OwnerId}" id="owner"/>
            <apex:inputField value="{!Account.MiddleName}" id="middlename"/>
            <apex:inputField value="{!Account.Phone}" id="phone"/>
            <apex:inputField value="{!Account.LastName}" id="lasname"/>
            <apex:inputField value="{!Account.PersonMobilePhone}" id="mobile"/>
            <apex:inputField value="{!Account.Suffix}" id="suffix"/>
            <!-- some other inputs, removed for clarity -->
        </apex:pageBlockSection>
     </apex:pageBlock>
</apex:form>

Code of the apex class

public class AddAccountController{
private ApexPages.StandardController controller;
private Account account;
private Id userId {get; set;}
public Boolean isBusinessAccount {get; set;}

public AddAccountController(ApexPages.StandardController controller){
    this.controller = controller;
    this.userId = UserInfo.getUserId();

    account = (Account) controller.getRecord();
    account.OwnerId = userId;
    System.debug('#MyLog: ' + Schema.SObjectType.Account.getRecordTypeInfosById().get(account.RecordTypeId).getName());
    isBusinessAccount = Schema.SObjectType.Account.getRecordTypeInfosById().get(account.RecordTypeId).getName() == 'Business Account';
} }

When I try with a Business Account, no issue

But when I try with a Person Account, the input fields are not rendered.

Picture of not rendered inputs

I checked on the recordtype id which fits the PersonAccount (in the url), I checked the sharing rules, it's visible...

My guess is that the VFP renders all the page based on the standard controller, and compiles it as the account being a Business Account..

Although in the debug log of the custom controller, it says that's it a PersonAccount

Why does it do so ? Thank you for the help

3

The only solution I found for this situation is to not use an extension but a custom controller. In the controller, create attributes for the "invisible" fields and call these fields in the visualforce page with InputText.

Example for the field firstName in the visualforce page :

<apex:pageBlockSectionItem>
    <apex:outputText value="First Name"/>
    <apex:outputPanel> 
        <apex:selectList value="{!salutation}" multiselect="false" size="1">
            <apex:selectOptions value="{!SalutationList}"/>
        </apex:selectList>
        <apex:inputText value="{!firstName}" id="firstname"/>
    </apex:outputPanel>
</apex:pageBlockSectionItem>

Don't forget to create a method for the selectList or you can hardcoded the value in the visualforce page. After that, Override the save method etc... (put the values of the attributes in the fields and upsert the account)

It's harder for the maintenance but I didn't find any other solution. If someone have another solution...

0

It appears to be an issue with field level security. To set field level security :

  1. Go To Setup->Customize->Account->Fields.(Not sure but Person account fields may appear at some separate place)
  2. Click on appropriate fields(fields which are not visible on your VF page).
  3. Click on 'Set Field Level Security' button.
  4. Make sure fields are visible to all appropriate profiles.
-1

Your problem is because for person accounts the firstName, lastName, and Salutation (among other fields) are stored in a contact record that is related to the account record. You could easily combine one account and one contact in your controller while saving the PersonAccount with the correct record type.

How to create a PersonAccount in salesforce using Apex code

  • If you read the answer on your link you will see that Account.FirstName is valid – Eric Sep 5 '16 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.