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
chris_centrachris_centra 

Issue with PersonAccount and inputField (firstname, lastname)

Hello.  I have a VF page where a user can enter search parameters for a PersonAccount.  In the controller, i instantiate a dummy Account - and use that for the inputField info (apex:InputField value="{!patientSearchAccount.LastName}".  The issue is that on the VF page, the firstname and lastname fields are "disabled" - presumably because the system doesn't know it's a person acct.  How do i make this work?

 

Note, i found a similar thread (http://boards.developerforce.com/t5/Visualforce-Development/Creating-a-Person-Account-with-Visualforce-and-a-Custom/m-p/137953) - but this question is never answered there. 

 

Thanks for your time.

chris

jwetzlerjwetzler

This is a bit of a shot in the dark because I haven't dealt with person accounts in a while and I don't have a test org handy to try it out with, but there are two fields of note on Account, one is isPersonAccount and one is RecordTypeId.  IsPersonAccount is just a boolean field that tells you which record type your account is using.  I believe the field is read only but you may want to try setting that to see if it works.  Otherwise if you can figure out what the recordTypeId is for person accounts you should be able to set that field.

chris_centrachris_centra

Hello - thanks for the reply.

 

right - IsPersonAcct is read-only.  I tried setting the RecordType to a PersonAccount record type, but that didn't change anything (presumably because i instantiated the Account, but didn't actually persist and re-load it...i'd think if i queried a PersonAccount, VF would know how to handle it, but just setting a RT on a instantiated PA doesn't seem to help).

 

Thanks

chris

donrdonr

I am having a similar issue and am very interested in ideas/thoughts on this. Here's similar issue regarding PersonRecords;

 

http://boards.developerforce.com/t5/Visualforce-Development/Creating-a-Person-Account-with-Visualforce-and-a-Custom/m-p/168753/highlight/false#M21182

 

 

chris_centrachris_centra

Nothing i'd call a great solution - or even an explanation so far - but a few observations.

 

In my code, i have a VF page where i want the user to enter PersonAcct info.  So i instantiate an Account, set a PA RecordType, and add fields to the page.  In addition to the fact that it the FirstName and LastName fields were disabled, it would throw an exception sometimes.  I've seen the same issue elsewhere on the forums - none of the threads had an explanation or a solution.  It complained about an invalid RecordType.  See the following thread to see someone else with the same issue: http://boards.developerforce.com/t5/Visualforce-Development/Setting-record-type-on-visualforce-page-creating-a-new-record/m-p/214261.

 

I tried many different things.  The following approach seems to "work" although it's not particularly pretty or clean.

 

1) instantiate an account - don't set a PersonAcct RecordType

2) for all PersonAccount specific fields (FN, LN, etc), just use separate fields no the contoller (lame, i know)

3) when saving the Account, set the RecordType.

 

Now - if i just stop there, when the page refreshes (if it's returning to the same page), i get the error again telling me that it's an invalid RecordType.  No idea why - because the record saved correctly and things appear to be in good order.

 

4) So immediately after inserting the Account, i set the RecordType back to null, and then when the page refreshes, all is well.  (so this is REALLY lame and messy - but i'm not sure what the problem is or how to legitimately fix it.)

 

Anyway, if there's a solution to this whole mess, please post it!

 

thanks

chris

donrdonr

Thanks for the fast reply..

 

I sorta understand what you did, but I'm not clear on your second point. Do you have  a simple code snippet /example  you can post?

chris_centrachris_centra

Hello.  When you say, "second point" - do you mean about using regular instance variables for the PersonAccount-specific fields?  If so, it means that for firstname and lastname, i just have string variables for them (outside of the Acct object) - and i reference them from VF just like i would any other string variables.  I've given up trying to figure out how to make it work within the PersonAccount, so i'll make it work outside of the PersonAccount.  Does that make sense?

thanks

chris

donrdonr

Thanks for the clarification, yes I understand  what you did. I'll give it a try, at least that will get me a little closer to a solution.. Thanks for the workaround. 

 

That said, it would be ideal if someone from Salesforce would respond and provide a solution that works using the account object. From the many posts  I've read there are a lot of people struggling with this issue.

chris_centrachris_centra

i'll keep working on it while i continue development.  if i learn anything, i'll post it.  please do the same.

thanks

chris

donrdonr

Chris, thanks for all of your help on this. I threw together the example below but it's generating "LastName" is missing error. 

 

Does this somewhat match your approach? I think there may be an issue with my insert logic. 

 

Any advice is appreciated!

 

 

Apex

 

 

                    <apex:inputText value="{!afirstName}"/>
                    <apex:inputText value="{!alastName}"/>

                    <apex:commandButton action="{!saveme}" value="Save" styleClass="btn"/>

 

Controller

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* This class is the controller behind the New PersonAccount 
*/

 Public with sharing class PersonAccountController {

 public Contact contact;
 public Account PersonAccount;

 public String afirstName;
 public String alastName;

 public String getAfirstname()
  {
    return afirstName;
  }

 public String getAlastname()
 {
   return alastName;
 }

 public void setAfirstName(String newFirstName)
 {
     afirstName=newFirstName;
 }

 public void setAlastName(String newLastName)
 {
    alastName=newLastName;
 }

 public void UseTextData()
 {
    List<Account> accounts=[select Id, Name from Account where Name=:alastName];
 }

 private Id queryForPersonRecType()
 {
    return [Select Id From RecordType where Name = 'Individual'].Id;
 }

 public Contact getContact(){
     if (contact==null) {contact = new Contact();
     }
    return contact;
 }

 public Account getPersonAccount() {
      if (PersonAccount == null) {PersonAccount = new Account ();
      }
      return PersonAccount;
   }

 public PageReference saveme() {

        Account personrec = new Account (FirstName=afirstName, LastName=alastName, recordTypeId = queryForPersonRecType());

         insert personrec;

  // send the user to the detail page
        PageReference providerPage = new PageReference('/' + PersonAccount.id);

        providerPage.setRedirect(true);
        return providerPage;
     }
 }

 

 

chris_centrachris_centra

curious that you're getting an error.  assuming that you're setting a value in the Last Name field, that looks like it should work.  i would add a debug line before the save to see that value is in the LastName field.  for some reason it's not getting set properly, i guess?

chris

donrdonr

Thought the same thing so I put this debug in. All values in the debug line at set properly.. I'm confused.

 

Error:

 

System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Last Name]: [Last Name] 

 

Controller Code:

 

 System.debug('***** BEFORE INSERT: First=' + afirstName + '--- Last=' + alastName + '--- Rectype=' + queryForPersonRecType());
        
 Account a = new Account (FirstName=afirstName, LastName=alastName, recordTypeId = queryForPersonRecType());
 insert a;


chris_centrachris_centra

Hmmm, i don't know why you're getting that error.  to be completely certain that the values are set, i'd log the Account before the insert, but if alastname is set in the previous line, that should be fine.

chris

StenEStenE

Hi Guys,

 

I have the exact same issue; the firstname, lastname field are non-editable. Did anyone find a solution and if not did anyone reported this as a bug?

 

Thanks,

Sten

donrdonr

If using a person account, I resolved this issue by Not setting the recordtype (set to null) prior to the insert. Also, be careful if you have vaidation rules related to the Account to be sure to satisfy those, that hung me up as well..

 

Example:

 

     PersonAccount = new Account ();


     PersonAccount.lastname  = alastName; 
     PersonAccount.firstname = afirstName;

            .

            .

            ect...

 

     Try {
            insert PersonAccount;
            }
            
      // Catch any errors detected by configured validations and display them on the form

      catch(DmlException ex)

            {
             ApexPages.addMessages(ex);
             return null;
            }