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
David Roberts 4David Roberts 4 

Command not updating fields on visualforce page

I have a custom object for territories holding postcode and manager of that area.
I want to search for a postcode/zip match and update the page with the result.
I'm using a command button to initiate the search.
My search is returning the correct result but not refreshing the other fields.
I guess I'm not using rerender correctly but can't get it to work.
Please help.

Here's the code:

<apex:page controller="TerritoryController" tabstyle="Territory__c">
    <apex:form >
        
        <apex:pageBlock title="Area Representative Lookup" id="theBlock">


            <apex:pageBlockSection columns="1" id="theSection">
                     


                
                <apex:outputText value="{!Area}"/>
                <apex:outputText value="{!Name}"/>
                <apex:outputText value="{!Manager}"/>
                <apex:outputText value="{!Postcode}"/>
                
                
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="search by postcode" for="searchText" />
                    <apex:inputText value="{!searchText}" id="SearchText"/>
                </apex:pageBlockSectionItem>
                
                
                
            </apex:pageBlockSection>

            <apex:pageBlockButtons >
                <apex:commandButton action="{!lookup}" value="Lookup"   rerender="theSection"/> 
            </apex:pageBlockButtons>
<!-- rerender="theBlock"  immediate="true" -->
        </apex:pageBlock>
        
    </apex:form>
</apex:page>

public class TerritoryController {
    // Called by apex page:
    // ...salesforce.com/apex/vwAreaRepLookup
    
    public List<Territory__c> myTerList;
    
    public string name;
    public string postcode;
    public string area;
    public string manager;
    public string searchText;
           
    
     //constructor
    public TerritoryController() {
        searchText = '';
    }



    public List<Territory__c> getmyTerList() {
        
        system.debug('in getmyTerList searchText=>'+searchText+'<');
        
        ApexPages.Message msgErr;
        ApexPages.Message msgInfo;
        String err = 'Hello';
        String info = 'Hi!';
        
//example error post
//err='whatever';
//msgErr = new ApexPages.Message(ApexPages.Severity.ERROR, err);
//ApexPages.addmessage(msgErr);

//example info post
//info = 'searchtext=>'+searchText+'<';
//msgInfo = new ApexPages.Message(ApexPages.Severity.INFO, info);
//ApexPages.addmessage(msgErr);

Territory__c[] myTerList = [Select territory__c.area__c, territory__c.postcode__c, territory__c.name, territory__c.territory_manager__c from Territory__c where postcode__c = :searchText ];
        return myTerList; 
            
            

    }//getMyRetLicList
    
    
    public String getManager() {
        return 'nothing';
    }

    public String getArea() {
        return 'nothing';
    }

    public String getName() {
        return 'nothing';
    }
    
    public String getPostcode() {
        system.debug('in get postcode');
        return 'nothing';
    }
    
    public String getSearchText() {
        return 'MK';
    }
    
    public void setSearchText(String theSearchText) {
        searchText = theSearchText;
    }
    
    public void lookup() {
        
        system.debug('in lookup searchText=>'+searchText+'<');
            
    
        Territory__c result = [Select territory__c.area__c, territory__c.postcode__c, territory__c.name, territory__c.territory_manager__c from Territory__c where postcode__c = :searchText limit 1];
        
        name = result.name;
        postcode = result.postcode__c;
        area = result.area__c;
        manager = result.territory_manager__c;
        system.debug('name: '+name);
    

    }//Lookup


}//TerritoryController
David Roberts 4David Roberts 4
I solved it but not really sure why. Seems to be the way I queried the database.
Anyway, in case it is of use to anyone, here is my code:-

<apex:page controller="TerritoryController" >
    <apex:form id="theForm">
        
        <apex:pageBlock title="Area Representative Lookup" id="theBlock">


            <apex:pageBlockSection columns="1" id="theSection">
                     

               <apex:pageBlockSectionItem > 
               <apex:outputLabel value="Area: "/>
                <apex:outputText value="{!area}"/>
                </apex:pageBlockSectionItem>
                
                <apex:pageBlockSectionItem >
                <apex:outputLabel value="Name: "/>
                <apex:outputText value="{!name}"/>
                </apex:pageBlockSectionItem>
                
                <apex:pageBlockSectionItem >
                <apex:outputLabel value="Manager: "/>
                <apex:outputText value="{!manager}"/>
                </apex:pageBlockSectionItem>
                
                <apex:pageBlockSectionItem >
                <apex:outputLabel value="Postcode: "/>
                <apex:outputText value="{!postcode}"/>
                </apex:pageBlockSectionItem>
                
                
                <apex:pageBlockSectionItem >
                    <apex:outputLabel value="search by postcode" for="searchText" />
                    <apex:inputText value="{!searchText}" id="SearchText"/>
                </apex:pageBlockSectionItem>
             
                
            </apex:pageBlockSection>

            <apex:pageBlockButtons >
                <apex:commandButton action="{!lookup}" value="Lookup"  /> 
            </apex:pageBlockButtons>
        </apex:pageBlock>
        
    </apex:form>
</apex:page>

public class TerritoryController {
    // Called by apex page:
    // ...salesforce.com/apex/vwAreaRepLookup
    
    
    public string name;
    public string postcode;
    public string area;
    public string manager;
    public string searchText;
           
    
     //constructor
    public TerritoryController() {
        searchText = '';
    }

 
    public String getManager() {
        return manager;
    }

    public String getArea() {
        return area;
    }

    public String getName() {
        return name;
    }

    
    public String getPostcode() {
        system.debug('in get postcode');
        return postcode;
    }
    
    public String getSearchText() {
        return searchText;
    }
    
    //required else considered read only
    public void setSearchText(String theSearchText) {
        searchText = theSearchText;
    }
    
    public void lookup() {
        
        List<Territory__c> lstTerritories = new List<Territory__c>();
        String AccntName;
        lstTerritories = [SELECT territory__c.area__c, territory__c.postcode__c, territory__c.name, territory__c.territory_manager__c, territory__c.territory_manager__r.name FROM Territory__c WHERE postcode__c = :searchText limit 1];
        
        System.debug('size = '+lstTerritories.Size()) ;      
        if (lstTerritories.Size()>0) {
            name = lstTerritories[0].name;
            postcode = lstTerritories[0].postcode__c;
            area = lstTerritories[0].area__c;
            manager = lstTerritories[0].territory_manager__r.name;
        }
        else {
            name = '';
            postcode = 'unrecognised postcode';
            area = '';
            manager = '';
        }


    }//Lookup


}//TerritoryController


/**
 * This class contains unit tests for validating the behavior of Apex classes
 * and triggers.
 *
 */
@isTest(SeeAllData=true)
private class vwAreaRepLookupTest {

    static testMethod void myUnitTest() {
        // TO DO: implement unit test
        PageReference pageRef = Page.vwAreaRepLookup;
        Test.setCurrentPage(pageRef);
        
        TerritoryController theCont = new TerritoryController();
        
        theCont.setSearchText('MK');
        theCont.Lookup();
        
        string man = theCont.getManager();
        string area = theCont.getArea();
        string name = theCont.getName();
        string zip = theCont.getPostcode();
        
        string st = theCont.getSearchText();
        theCont.setSearchText('YY');
        theCont.Lookup();
          
    }
}