1

Here's the piece of my markup that includes the inputField:

<apex:form >
        <apex:pageBlock mode="edit" rendered="{!reportAppRendered}">
            <apex:inputField value="{!selecOptionApp.Reported_Issue__c}" style="width: 300px; height: 100px" required="true"/>

            <c:MultiselectPicklist leftLabel="Available App Servers"
                                   leftOptions="{!allAppServers}"
                                   rightLabel="Selected App Servers"
                                   rightOptions="{!selectedAppServers}"
                                   size="7"
                                   width="125px"/>

            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Save" action="{!saveAppMultiselect}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
</apex:form>

Here are the relevant parts of my controller:

public with sharing class ItemEditController {

  private ApexPages.StandardController controller {get; set;}
  public List<Account> searchResults {get;set;}
  public App_Server__c selectOptionApp{get;set;}

  public ItemEditController(ApexPages.StandardController controller) {}

  public PageReference saveAppMultiselect() {

      selectOptionApp  = [SELECT Id,Reported_Issue__c,Issue_Reported_Date__c,Issue_Reported__c FROM App_Server__c WHERE Name = 'Temporary VF App Server Record'];
      update selectOptionApp;      

      return null;       
  }

  public PageReference search() {

    String qry = 'SELECT Id, Name, Issue_Reported__c, Issue_Reported_Date__c, Reported_Issue__c FROM Account WHERE Name LIKE \'%'+searchText+'%\' order by name';
    searchResults = Database.query(qry);

    return null;
  }

  // fired when the save records button is clicked
  public PageReference save1() {

    try {
      update searchResults;  
    } Catch (DMLException e) {
      ApexPages.addMessages(e);
      return null;
    }

    String qry = 'SELECT Id, Name, Issue_Reported__c, Reported_Issue__c FROM Account WHERE Name LIKE \'%'+searchText+'%\' order by name';
    searchResults = Database.query(qry);

    return null;
  }    
}

I let the searchResults variable and relevant methods remain in my snippet only to say that searchResults does successfully get updated with inputField values, so I don't see why selectOptionApp doesn't when you compare the logic.

The most substantial differences I see are:

  • selectOptionApp is an object while searchResults is a List of objects
  • searchResults: query assignment and update are in separate methods
  • selectOptionApp: query assignment and update are in the same method.

But I don't think any of that is significant. Am I wrong?

What may I be missing?

2
  • 1
    Action methods do not require a PageReference return type. This can simplify your code considerably, and it makes your intent clearer, since you're not redirecting anywhere.
    – sfdcfox
    Jul 30, 2018 at 1:06
  • 2
    Also, I strongly recommend using inline queries [select ... from ... where ...] instead of using Database.query when you don't need to for performance and regression checking reasons.
    – sfdcfox
    Jul 30, 2018 at 1:07

1 Answer 1

3

You're refreshing selectOptionApp from the database every time saveMultiAppSelect() gets called:

  selectOptionApp  = [SELECT Id,Reported_Issue__c,Issue_Reported_Date__c,Issue_Reported__c FROM App_Server__c WHERE Name = 'Temporary VF App Server Record'];
  update selectOptionApp;      

As a result, any changes made to the sObject variable due to Visualforce binding to that input field are wiped out, and the update operation is a no-op.

Remove the query (execute the query only when you need to load fresh values from the database) and you should see the results you expect.

2
  • So the reason I tried querying a real record first was because I kept getting a NullPointerException at update selectOptionApp. Jul 30, 2018 at 1:06
  • 1
    That's a different problem. You need to make sure selectOptionApp is populated. Your query looks like it's not determined by controller state, so it could potentially be queried in your constructor.
    – David Reed
    Jul 30, 2018 at 1:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .