0

I need to be able to update ONLY the website and BillingAddress fields. As of right now I can't get any of the fields to update.

APEX:

public class AccountTypeController 
{
    public Account filterAccount{get; set;}
    public Account[] recordList{get; set;}
    public Account[] updateList{get; set;}
    public List <Account> acc {get;set;}

    public AccountTypeController()
    {
        filterAccount = new Account();
        recordList = new List<Account>();
        acc= Database.query('select Name,id from Account Limit 10');
    }


    public void updateRecordList()
    {
        recordlist = new List<Account>();
        recordList=[Select Name, Type, Website, BillingStreet, BillingCity, BillingState, 
                    BillingPostalcode, BillingCountry 
                    From Account Where Type = :filterAccount.Type];
          if(recordList.size()>0) 
          {
            filterAccount = recordList[0];         
          }

    }



    public pageReference save()
    {
        updateList = [Select ID, Name, Type, Website, BillingStreet, BillingCity, BillingState, 
                    BillingPostalcode, BillingCountry From Account 
                     Where Account.ID =:Apexpages.currentPage().getParameters().get('id')];
        try
        {
            update updateList;
        }
        catch(Exception e)
        {
            System.debug('Exception occurred ' + String.valueOf(e));
        }    
            return null;
    } 

}

VF:

<apex:page controller="AccountTypeController">

    <apex:form >

        <apex:outputPanel styleClass="requiredInput" layout="block" id="pwPanel">

            <apex:pageBlock title="Filter Accounts" mode="edit">

                <apex:pageBlockSection >

                    <apex:inputfield value="{!filterAccount.Type}" label="Account Type">

                       <apex:actionSupport event="onchange" action="{!updateRecordList}" rerender="pwPanel"/>

                    </apex:inputfield>
                    <br/><br/><br/>
                  <apex:outputPanel rendered="{!recordList.size > 0}">

                    <apex:pageBlockTable value="{!recordlist}" var="rl">
                       <apex:inlineEditSupport showOnEdit="saveButton"
                                               hideOnEdit="editButton" event="ondblclick"/>
                            <apex:column value="{!rl.Name}"/>&nbsp;
                            <apex:column value="{!rl.Type}"/>&nbsp;
                            <apex:column value="{!rl.Website}"/>&nbsp;
                            <apex:column headerValue="BillingAddress"
                            value="{!rl.BillingStreet} {!rl.BillingCity}, {!rl.BillingState} {!rl.BillingPostalCode} 
                            {!rl.BillingCountry}"/>

                    </apex:pageBlockTable> 

                   <apex:commandButton action="{!save}" id="SaveButton" value="Save"/>

                  </apex:outputPanel>

                </apex:pageBlockSection>

            </apex:pageBlock>  

         </apex:outputPanel>

    </apex:form>

</apex:page>
  • This is 2 times controller class – utm Jul 24 '19 at 16:28
  • Sorry about that. I fixed it – Tyler Jul 24 '19 at 16:45
  • 1
    your save method is simply querying the database and updating the identical data - it ignores any changes done on the web page – cropredy Jul 24 '19 at 17:10
0

In your save() method, you are querying for a single account, based on a record Id in a URL parameter. There are a couple problems with this:

  1. When you query the record(s), you are getting them as they exist in the database, without any of the changes the user might have made in the Visualforce page. It makes no sense to query records, just to save them again in exactly the form you just retrieved them, but that is what your save() method is doing.
  2. You're working with a list of records in the page, but then only selecting one record in your save() method. You'd want to be saving all the records at once.

The records you're listing in your pageBlockTable are represented by the recordList variable you've declared in your controller. The {get; set;}, called the "getter" and "setter", allows the VF page to get the value of that variable to display it, and then also to set it when you call a controller method from an action attribute in the page. When you call the save() method from the button, data in your VF page that has setters will be updated in those variables, and then you can save it to the database. So your save() method can be:

 public pageReference save()
    {
        try
        {
            update recordList;
        }
        catch(Exception e)
        {
            System.debug('Exception occurred ' + String.valueOf(e));

        }    
        return null;
    }

One problem with this code is that the try/catch block has the effect of suppressing any error notification to the user, and only putting the error in the debug log. You should add an <apex:page:messages /> tag to the VF and then use addError() to let the user know.

Additionally, per this answer, you can't use <apex:column value="{!obj.fieldName}" /> with inline editing (even though it looks like it works). You have to use <apex:outputfield> inside the columns:

 <apex:pageBlockTable value="{!recordlist}" var="rl">
      <apex:inlineEditSupport showOnEdit="saveButton" event="ondblclick"/>
      <apex:column headerValue="Name">
           <apex:outputField value="{!rl.Name}"/>
      </apex:column>
      <apex:column headerValue="Type">
           <apex:outputField value="{!rl.Type}"/>
      </apex:column>
      <apex:column headerValue="Website">
           <apex:outputField value="{!rl.Website}"/>
       </apex:column>
       <apex:column headerValue="BillingAddress" value="{!rl.BillingStreet} {!rl.BillingCity}, {!rl.BillingState} {!rl.BillingPostalCode} {!rl.BillingCountry}"/>
 </apex:pageBlockTable> 

More generally: Salesforce StackExchange is not a good place to learn how to code on the platform. I don't mean to say that the community doesn't welcome beginners, because we absolutely do. But over the past couple of days, you've posted variations on the same code samples that display a real lack of understanding of the fundamentals, like how VF pages and controllers communicate. I strongly recommend heading to Trailhead, Salesforce's really excellent online learning platform, and working through the Beginner and Intermediate Developer trails. This should help you get some of these basic notions down. And if you get stuck, do some searching, check the docs and other resources that are linked from the Trailhead modules, and then feel free to come back here and ask some specific questions about where you're stuck.

  • Yeah I've done all the beginner developer trails and started on the intermediate. I have a beginner level understanding of Java but have never worked with webpages. I realized the query was getting the same results from the database I was just hoping someone could tell me how to get the results from the user input and update the records in the database. I've looked at other docs and have tried various different code. I actually had the code you suggested before i posted and it wasnt working either. – Tyler Jul 24 '19 at 18:04
  • Interesting! I found this answer that solves it: salesforce.stackexchange.com/a/107253/3934 Even though it looks like it works, you can't use <apex:column value="{!obj.fieldName}" /> with inline editing. You have to use <apex:outputfield>s. I'll add VF code sample to my answer. – Thomas Taylor Jul 24 '19 at 19:00
  • That makes absolutely no sense why it has to be in an outputField but Thanks for the help! So many weird things im learning about in this language. Are there free tutorials besides trailhead? – Tyler Jul 24 '19 at 19:17
  • Looking again at the docs for inlineEditSupport, it does say right at the top that it "provides inline editing support to <apex:outputField> and various container components." So it's odd that it looks like it works with value attributes of columns, but not odd that it doesn't in fact work. – Thomas Taylor Jul 24 '19 at 19:31

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.