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
clstanton007clstanton007 

Using USPS Zip Code Lookup to populate city and state from entered zip code

I am relatively new to web services, but not new to Apex/SFDC.  I was looking for some resources on how to call the USPS web service and populate city and state when a user enters a zip code, and populate the city and state in "real time".  Is this possible?  Any info would be greatly appreciated!

kyle.tkyle.t
clstantonclstanton

Hey Kyle, yes I did see those tools available.  I guess I wasn't totally clear - I guess a more accurate question is:  how do I call a WS from Apex, and update a field in real time?  I guess there are different ways to do it, and it looks as though using wsdl2apex is the most straightforward.  I kind of get that - the wsdl basically generates  the methods that you can call.  But how do I have the city and state update in real time with the response from the web service?

 

Thanks

kyle.tkyle.t

I have actually never written a web service, so I am not totally sure, but from my quick reading I believe what you do is use the WSDL to create the necessary classes. From there you can create a new class based on that WSDL in your code.  Get the zip code from the record and invoke the web service to get the city and state.

 

Some high level pseudo code, I hope that this points you in the right direction:

String zip_code = yourObject.PostalCode;

USPSClass uspsLoopup = new USPSClass();

yourObject.city = USPSClass.getcity(zip_code);
yourObject.state = USPSClass.getState(zip_code);

update object;