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
dfiredfire 

error creating a Person Account in a custom controller

I am creating an online application that will dump into a Person Account. However when I try to instanciate the account obj in the getter I get the following runtime validation error:

 

Validation Errors While Saving Record(s) There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Student Record Type: value not valid for the entity: Account".

 

Here's the VF page

 

 

<apex:page showHeader="false" controller="newStudentApplicationController" >
  <apex:sectionHeader title="Application" subtitle="Page 1 of 5"/>
  <apex:form >
      <apex:pageBlock title="Student Application" mode="edit">
         <apex:pageBlockButtons >
             <apex:commandButton action="{!save}" value="Save"/>
             <apex:commandButton action="{!cancel}" value="Cancel" 
                              onclick="return confirmCancel()" immediate="true"/>

         </apex:pageBlockButtons>
         <apex:pageBlockSection title="General Information">
             <apex:inputfield id="studentTitle" value="{!student.salutation}"/>
             <apex:inputfield id="studentFirstName" value="{!student.firstName}"/>
             <apex:inputfield id="studentLastName" value="{!student.lastname}"/>
             <apex:inputfield id="studentBirthdate" value="{!student.personBirthdate}"/>
             <apex:inputfield id="studentAffiliation" value="{!student.Affiliation__pc}"/>
         </apex:pageBlockSection>
      </apex:pageBlock>
  </apex:form>
</apex:page>

 

 

 

Here is the Controller

 

 

public class newStudentApplicationController {
    // Object Variable
    Account student;
    
    // Getters
    public Account getStudent() {
        if(student == null) {
            RecordType recType = [select id,name,sobjectType,ispersontype from recordType where ispersontype=true and sobjectType='account' limit 1];            
           // student = new Account(recordtypeid=recType.id); 
            student = new Account(); 
        }
        return student;
    }
    

 

If I try using the default constructor (commented out line above), I this error:

 

 

Validation Errors While Saving Record(s) There were custom validation error(s) encountered while saving the affected record(s). The first validation error encountered was "Student Record Type: value not valid for the entity: PersonAccount".

 

I have a feeling I am missing somethign small. Any help would be GREATLY Appreciated.

 

Thanks!

gtuerkgtuerk

This isn't your full code.  Two things I'd do, one to fix your issue, the other to modularize your code better.  First, to fix your issue make sure all profiles using this page have access to the RecordTypes you've defined for the Account object.  When you set up the record type, you are given the option to enable for all or select profiles.  you probably missed it for one or more profiles.

 

The second thing I'd do is move your selection of a recordtype into a utility, rather than querying for it in-line and in a getter.  If your code gets to any reasonable level of complexity, this will be an important encapsulation technique

dfiredfire

Gtuerk, thanks for the response. I am currently working in DE and all off my profiles have the Person Account record type enabled and not only that but it is the only record type enabled. Also I am working with a system admin profile, so if that was the issue it should still have worked for me, no?

 

You are correct, this is not my full code as the rest is somewhat irrelevant as it is just the save method, which has yet to be called and since this was a runtime issue, not a compiler issue, I didn't think it was necessary. If you want to see it, I can show you.

 

Thanks for the using the util tip. Any other ideas of the problem?

 

Here is the controller again

 

public class newStudentApplicationController {
    // Object Variable
    Account student;
    RelativeContact__c spouse;
    RelativeContact__c father;
    RelativeContact__c mother;
    List<Educational_Institution__c> schools;
    
    // Other Variables
    String cancelPageURL = 'http://www.google.com';
    String submittedPageURL = 'http://www.google.com';
    
    // Getters
    public Account getStudent() {
        if(student == null) {
            RecordType recType = [select id,name,sobjectType,ispersontype from recordType where ispersontype=true and sobjectType='account' limit 1];            
           // student = new Account(recordtypeid=recType.id); 
            student = new Account(); 
        }
        return student;
    }
    
    public RelativeContact__c getSpouse() {
        if(spouse == null)
            spouse = new RelativeContact__c();
        return spouse;
    }
    
    public RelativeContact__c getFather() {
        if(spouse == null)
            spouse = new RelativeContact__c();
        return spouse;
    }
    
    public RelativeContact__c getMother() {
        if(spouse == null)
            spouse = new RelativeContact__c();
        return spouse;
    }
    
    // Page References for Navigation
    public PageReference page1() {
      return Page.ApplicationPage1;
    }

    public PageReference page2() {
      return Page.ApplicationPage2;
    }

    public PageReference page3() {
      return Page.ApplicationPage3;
    }

    public PageReference page4() {
      return Page.ApplicationPage4;
    }

    public PageReference page5() {
      return Page.ApplicationPage5;
    }

    // If applicant cancels, redirect to cancel page
    public PageReference cancel() {
        PageReference cancelPage = new PageReference(cancelPageURL);
        cancelPage.setRedirect(true);
        return cancelPage; 
    }
    
    // Save the application into Salesforce
    public PageReference save() {
        
        insert student;
        
        PageReference submittedPage = new PageReference(submittedPageURL);
        submittedPage.setRedirect(true);
        return submittedPage; 
    }

    
}