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
Hazel LarotHazel Larot 

How do I use actionSupport in existing code to autofill City and State using my custom Zip Code object

I am trying to update an existing code in my org to do some zip code lookup. We have an existing visualforce page with a field that asks for a zip code. I want to convert this to a lookup field that returns a list of City and State combination (example: Boston, MA) matching the zip code in my zip code custom object (ZipCode__c) and populates the City and State fields when user selects the city/state combination.

I read that I can use actionSupport to do this. I have very little experience in apex/vf so I hope somebody can help with the code.

Here's my org's existing VF page code for zip code input:
<apex:pageBlockSectionItem > 
    PG Zip: <apex:inputtext value="{!PGZip}"/> 
</apex:pageBlockSectionItem>
In the controller, the PGZip and the city and state fields are declared/saved like below:
public String PGZip{get;set;} 
PG_ct.MailingPostalCode = PGZip; 
myPageCon.PGZip = '21345'; 

public String PGCity{get;set;} 
PG_ct.MailingCity = PGCity; 
myPageCon.PGCity = 'Boston'; 

public String PGState{get;set;} 
PG_ct.MailingState = PGState; 
myPageCon.ECState = 'MA';

 
Alastair PhippsAlastair Phipps
Hi Hazel,

I'm not sure I'm quite clear on your objective here. Do you want to have a single field 'City, State' and have that autofill three other fields 'City', 'State' and 'Zip'? Or do you want one field 'Zip' to then autofill two other fields 'City' and 'State'?

Either way you're going to need a massive Zip lookup somewhere. Do you already have that in your setup somewhere else?

A.
Hazel LarotHazel Larot
Hi Alastair,
Thanks for the reply.
I want the latter, for a user to enter the zip code, then the separate fields for City and State (in the Contact object) to be autofilled with the matching City and State data from the existing ZipCode custom object. I read somewhere that I could use USPS's zip code lookup service, but since we have an existing ZipCode custom object that contains Zip Code, City and State data, I could use this instead to loop up the matching the city and state fields. Hope that makes sense. 
Alastair PhippsAlastair Phipps
I'm not too hot on my apex, but it sounds to me like you want a zip lookup field from your custom object (the one with zip, city and state) on the visualforce page, so something like this: <apex:inputField value="{!customobject.zip}"/>

After that (with my limited knowledge) I'd either be using formula fields or a workflow rule to set the other field values rather than relying on apex. That also has the added bonus of being easier to edit later!

A.