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
Raghu_devRaghu_dev 

How to access master picklist value in the controller ?

Hello wonderful developers out there !! I have been breaking my head for the past three days to get this done but kind of gave up now. I am a medium to advanced developer/analyst (with all the great help from the community) and got stuck with this special problem. I am trying to develop something like address validation (not with any external webservice etc) using some data loaded from a thirdparty vendor into custom object. I managed to accomplish the solution(almost 95%)  using visualforce but got stuck with this one small issue.

 

I have 4 dependent fields - Country, State and City and Zipcode fields for which i am using one custom component (developed by another great developer from the community) to show city names when the user starts typing in city name after selecting Country and State. All this works absolutely fine till that point. Problem starts when user picks the city name from the dropdown when I was expecting zipcode rerender but somehow that rerender is not happening. This is happening when I try to create a new record(editing of the existing record works fine). One thing i observed from the debug logs is the master picklist value(in this case, State) is going back as null to the controller.  This is the code snippet  i have been using  and please help me.

 

Debug log : 

 

17:30:52.262|METHOD_ENTRY|[80,7]|system.debug(String)
17:30:52.263|USER_DEBUG|[80,7]|DEBUG|This.State value now null

 

Apex Page:

 

<apex:page standardController="Account" extensions="AccountController" tabstyle="Account" standardStylesheets="true">
    
    <script type="text/javascript" src="{!URLFOR($Resource.autocompletejs)}" />
	<apex:stylesheet value="{!$Resource.autocompletecss}"/>	
	
	<style>
	
		.cField{
			width:300px;
		}
	</style>
    <script>

		//This is a small workaround for getting the component to work with the enter key
		//I'll include this behavior as part of the component in a next release
 		function noenter(e){
 
		     var key;    
		     if(window.event){
		          key = window.event.keyCode; //IE
		          //alert ('key entered - '+ key);
		     } 
		     else
		          key = e.which; //firefox    
		
		     return (key != 13);	

 		}
 		
       	function disableautocomplete() {
		  var forms = document.getElementsByTagName('form');
		  
		  for (var i = 0; i < forms.length; ++i) {
		    if (forms[i].setAttribute) {
		      forms[i].setAttribute('autocomplete', 'off')
		    } else {
		      forms[i].autocomplete = 'off'
		    }
		  }
		}
		
		if (window.addEventListener) {
		  window.addEventListener('load', disableautocomplete, true)
		} else if (window.attachEvent) {
		  window.attachEvent('onload', disableautocomplete)
		} else {
		  window.onload = disableautocomplete
		}
	</script>
    <apex:form id="addressForm">
        <apex:pageBlock mode="edit" id="thePageBlock">
            <apex:pageMessages />
            <apex:pageBlockSection title="Account Information" columns="2" id="theInfoSection">
                 <apex:inputField id="accname" value="{!Account.name}"/>
               	 <apex:inputField id="accowner" value="{!Account.Owner.Name}"/>
                 <apex:inputField id="accphone" value="{!Account.Phone}"/>
                 <apex:inputField id="accfax" value="{!Account.Fax}"/>
                 <apex:inputField id="accwebsite" value="{!Account.Website}"/>
                 
            </apex:pageBlockSection> 
            <apex:pageBlockSection title="Address Information" columns="1" id="theSection">
                	
                <apex:actionRegion >
	                <table class="detailList">
	                   <tbody>
	                    <tr> 
	                   	    <td class="labelCol first">Street Address</td>
	                   		<td class="data2Col first">
	                   			<apex:inputField id="theAddress1" value="{!Account.MODS_Address__c}" required="true"/> 
	                   		</td>
	                   	</tr>
	                   	<tr> 
	                   	    <td class="labelCol first">P.O Box(Address 2)</td>
	                   		<td class="data2Col first">
	                   			<apex:inputField id="theAddress2" value="{!Account.MODS_Address_2__c}" required="false"/> 
	                   		</td>
	                   	</tr>
	                   	<tr> 
	                   	    <td class="labelCol first  ">Country </td>
	                   		<td class="data2Col first">
	                   			<apex:inputField id="theCountry" value="{!Account.MODS_Country__c}" required="true"/> 
	                   		</td>
	                   	</tr>
		                <tr>
					       <td class="labelCol first  ">State </td>
					       <td class="data2Col first">
					        	<apex:actionRegion >  
			                    <apex:selectList id="theState" value="{!account.MODS_State__c}" required="true" size="1">
			                    	<apex:selectOptions value="{!States}"/>
			                		<apex:actionSupport event="onchange" action="{!resetCity}" rerender="cityDisplay"/>
			                	</apex:selectList>
			                	</apex:actionRegion>
	                	   </td>
	                		
	                	</tr>
	                	</tbody>
	                </table>	
                </apex:actionRegion> 
                <apex:outputPanel id="cityDisplay" layout="block" style="font-weight:bold">
                   <table class="detailList">
                   <tbody>
	                <tr>
				       <td class="labelCol first  ">City</td>
				        <td class="data2Col first  ">
				            <apex:inputField onkeypress="return noenter(event)" value="{!Account.MODS_City__c}" id="cityname" styleClass="cField" required="true">
	           					<c:AutoComplete Width="200" ClassName="autocomplete" InputId="{!$Component.cityname}" AutoCompleteId="accACid" ObjectName="Vertex_Data__c" StateName="{!$Component.theState}"/>
	           					<apex:actionSupport event="onblur" action="{!resetZip}" rerender="zipDisplay"/>
	           				</apex:inputField>      
				        </td>
				        <!-- <td class="data2Col first  ">
				        	<apex:commandButton value="Get Zipcodes" action="{!doNothing}" rerender="zipDisplay"/>
				        </td>
				         -->
				    </tr>
				       
				    </tbody>
				  </table>
				 </apex:outputPanel> 
				 
				 
                  <apex:outputPanel id="zipDisplay" layout="block" style="font-weight:bold">
                  <table class="detailList">
	                  <tbody>
		                  <tr>
						       <td class="labelCol first">Zipcode</td>
						        <td class="data2Col first">
						            <apex:selectList id="theZip" value="{!account.Zip_code_Postal_code__c}" size="1" required="false">
					                    <apex:selectOptions value="{!Zips}"/>
					                </apex:selectList>      
						        </td>
						    </tr>       
					    </tbody>
				  </table>
				  </apex:outputPanel>	
             </apex:pageBlockSection>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton value="Save" action="{!Save}"/>
                <apex:commandButton value="Cancel" action="{!Cancel}"/>
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
			
</apex:page>

Controller code :  

 

 

public class AccountController {
	//private final ApexPages.standardController theController;
    private final Account account; //{get { theController. } private set;}
    /*public String stateVal{get {
    	return account.MODS_State__c;
    }}
    public void setStateVal(String s){
    	account.MODS_State__c = s;
    }
    public String zipVal{ get {
    	return account.Zip_code_Postal_code__c;
    	}
    }
    
    
    public void setZipVal(String s){
    	account.Zip_code_Postal_code__c = s;
    }
    */
    /*
    
    public String getState() { return state; }
    public void setState(String s) { 
    	this.account.MODS_State__c = s; 
    	state=s;
    	}
    */
    /*String city;
    public String getCity() { return this.account.MODS_City__c; }
    public void setCity(String s) { this.account.MODS_City__c = s; }
	*/
    public AccountController(ApexPages.StandardController stdController) {
    	//this.account = (Account)stdController.getRecord();
        //Id id = ApexPages.currentPage().getParameters().get('id');
        this.account = stdController.getId() != null ? 
        						[SELECT  
                                    a.Name,
                                    a.MODS_Address__c, 
                                    a.MODS_Address_2__c, 
                                    a.MODS_City__c, 
                                    a.MODS_State__c, 
                                    a.MODS_Country__c,
                                    a.Zip_code_Postal_code__c,
                                    a.Owner.Name,
                                    a.Phone,
                                    a.Fax,
                                    a.WebSite
                                   FROM account a WHERE a.id = :stdController.getId()]:
        						new Account();
        
       
    }
	 
	public Account getAccount() {
            return account;
    }
	
	/*public String getStateVal(){ return stateVal;}
	public void setStateVal(String stateVal) { 
		system.debug('Inside setter, String S value ' + stateVal);
		account.MODS_State__c = stateVal; 
	}
	*/
   public void resetCity(){
   		account.MODS_City__c = null;
   		account.Zip_code_Postal_code__c = null;
   }
  
   public void resetZip(){
   		account.Zip_code_Postal_code__c = (account.Zip_code_Postal_code__c != '- None -' ) ? account.Zip_code_Postal_code__c : null ;
   }
   
   public list<AggregateResult> lstAR = new list<AggregateResult>();
   
   public List<SelectOption> getZips() {
      List<SelectOption> optionList = new List<SelectOption>();
      optionList.add(new SelectOption('', '- None -')); 

      
      system.debug('This.State value now ' +  this.account.MODS_State__c);
      if(this.account.MODS_State__c != null) {
      //if(state != null) {
        for (Vertex_Data__c vd : [select Postal_Code__c from Vertex_Data__c where State__c =: this.account.MODS_State__c and City__c = : this.account.MODS_City__c]){ 
          optionList.add(new SelectOption(vd.Postal_Code__c,vd.Postal_Code__c));
        }
      }
      return optionList;
     
   }

   public List<SelectOption> getStates() {
      List<SelectOption> optionList = new List<SelectOption>();
      
      optionList.add(new SelectOption('','- None -'));
	  optionList.add(new SelectOption('PA','PA'));
	  optionList.add(new SelectOption('CT','CT'));
      return optionList;   
      /*lstAR = [SELECT State__c FROM Vertex_Data__c GROUP BY State__c];  
	  
      
      for (AggregateResult vd : lstAR){ 
        optionList.add(new SelectOption((String)vd.get('State__c'), (String)vd.get('State__c')));
      }
      */  
    }
    
    /*public PageReference searchZips() {
      //List<SelectOption> optionList = new List<SelectOption>();
      
      optionList.add(new SelectOption('', '- None -')); 

      system.debug('State value now ' + stateVal);
      system.debug('This.State value now ' +  account.MODS_State__c);
      if(stateVal != null) {
      //if(state != null) {
        for (Vertex_Data__c vd : [select Postal_Code__c from Vertex_Data__c where State__c =: stateVal and City__c = : this.account.MODS_City__c]){ 
          optionList.add(new SelectOption(vd.Postal_Code__c,vd.Postal_Code__c));
        }
      }
      return null;
     
    }*/
    public PageReference doNothing() {
    	return null;
    }
   /* 
    public List<SelectOption> getCities() {
      List<SelectOption> optionList = new List<SelectOption>();
      
       optionList.add(new SelectOption('', '- None -')); 

      
      if(account.MODS_State__c != NULL) {
        for (Vertex_Data__c vd : [select City__c from Vertex_Data__c where State__c =: this.account.MODS_State__c]){ 
          optionList.add(new SelectOption(vd.city__c,vd.City__c));
        }
      }
      return optionList;
    }
	*/
	
	public PageReference save() {
      try {
      	  if (this.account.id == null) {
      	  	insert account;	
      	  }
          else
           update account;
           
       } catch(System.DMLException e) {
           ApexPages.addMessages(e);
           
       }
       //  After Save, navigate to the default view page:  
       
       //theController.save();
       return (new ApexPages.StandardController(account)).view();
   }
   public PageReference cancel() {
       PageReference accountPage = new ApexPages.StandardController(account).view();
       accountPage.setRedirect(true);
       return accountPage;
   }
}