0

I have created a controller to be able to edit person accounts, however I am using inputText vs inputField because of other customizations.

How can I autopopulate person account fields into my "inputTexts" when a user goes to edit the account? It appears blank and then when a user saves it overwrites the values with blank values.

<apex:pageBlock Title="edit Account">

    <apex:pageMessages />
    <apex:pageBlockSection Title="Information" columns="2">
        <apex:inputText label="First Name" value="{!fname}" id="fname"/>
        <apex:inputText label="Last Name" value="{!lname}" id="lname" html- 
        placeholder="{!acc.LastName}"/>
        <apex:inputText label="Email" value="{!acc.Personemail}" id="email" 
        html-placeholder="{!acc.personEmail}"/>

    </apex:pageBlockSection>
    <apex:commandButton Value="Save" action="{!saving}"/>
</apex:pageBlock>

Controller:

    Id accId;
    public Account acc     {get;set;}
    public string fname    {get;set;}
    public string lname    {get;set;}

    public AccController() {

        accId = ApexPages.currentPage().getParameters().get('accId');

        if (accId == null) {

            acc = new Account();

            return;

        }

        acc = [select id, FirstName, LastName, BillingCity, BillingStreet,BillingPostalCode, BillingCountry, 
                    PersonEmail from Account where id = :accId];

       }


        public PageReference saveThis() {
            acc.FirstName = fname;
            acc.LastName = lname;
            acc.PersonEmail = acc.Personemail;

            update acc;
        }
  • 2
    Going to need to see some code in order to be able to help you. Page & controller should do it. Should edit your question to include it. – battery.cord May 22 '18 at 14:57
  • added parts of my code. – adming32442 May 22 '18 at 15:32
1

You dont need to use fname and lname in your apex controller. You can direcly bind it to the account fields.

public class AccController{

    Id accId;
    public Account acc     {get;set;}     

    public AccController() {
        accId = ApexPages.currentPage().getParameters().get('accId');
        if (accId == null) {
            acc = new Account();
            return;

        }
        acc = [SELECT id, FirstName, LastName, BillingCity, BillingStreet,BillingPostalCode, BillingCountry,
                PersonEmail FROM Account WHERE id = :accId];
       }



    public PageReference saveThis() { 
        update acc;
        return null;
    }
}

And in your VF markup directly refer the account fields.

<apex:pageBlock Title="edit Account">

        <apex:pageMessages />
            <apex:pageBlockSection Title="Information" columns="2">
            <apex:inputText label="First Name" value="{!acc.FirstName}" id="fname" html-placeholder="Enter your FirstName"/>
            <apex:inputText label="Last Name" value="{!acc.LastName }" id="lname" html- 
            placeholder="Enter your lastname"/>
            <apex:inputText label="Email" value="{!acc.Personemail}" id="email" 
            html-placeholder="Enter your email id"/>

        </apex:pageBlockSection>
         <apex:commandButton Value="Save" action="{!saveThis}"/>
</apex:pageBlock>
| improve this answer | |
0

You never set the variables you use on the page. When a user loads the page, they'll be blank. If they don't change them, you'll end up with blank values in your database (assuming no validation or required fields). I wouldn't rely on html-placeholder for this, I'd set your variables in your controller. Personally, I prefer using lazy-loading getters for this, but you can also just set these variables in your constructor.

Lazy-loading:

public String FirstName {
    get {
        if (FirstName == null) {
            FirstName = account.FirstName; 
        }

        return FirstName; 
    }
}

In constructor:

if (accId == null) {
    account = new Account();

    FirstName = ''; 
} else {
    account = [ ... ];

    FirstName = account.FirstName; 
}

If you were using apex:inputField, you could skip this and just bind to the field, but since your using apex:inputText with some variables, you have to do a bit of heavy lifting yourself.

| improve this answer | |

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