You need to sign in to do that
Don't have an account?
Zip code wildcard search revisited
Hello, I have a lead zip code search Apex Controller and Visualforce page that I have been working on intermittently. I have posted other questions on this in the past, and have been able to refine the search as only numbers. However, it still gives random values for the number searches. I logged a case and they said to use a LIKE command with OR and include each searchable value. I have tried this but keep getting “unexpected token” errors when I compile.
Everything else is now working but I can’t seem to get it to do a wildcard search or to give exact values when they are entered for the search. Here is the controller. I left the LIKE '985%%' in there because it will match this exact value but without it I get random values.
public class zipController { public String searchResults { get; set; } public String displayResults { get; set; } String searchText; List<Lead> results; public String getSearchText() { return searchText; } //get results of query public List<Lead> getResults() { return results; } public PageReference doSearch() { //query and messages for results or no results results = [SELECT Company, Name, Email, PostalCode FROM Lead WHERE PostalCode LIKE '985%%' LIMIT 1000]; apexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, searchText )); if (results.size() == 0) {apexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, 'Sorry,no results for your current zip code search. Make sure you are using the proper format (99999)')); } else apexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, 'Here is your zip code search results')); return null; } }
Here is the Visual Force page:
<apex:page controller="zipController"> <apex:form > <apex:pageMessages /> <apex:pageBlock mode="edit" id="block"> <apex:pageBlockSection > <apex:pageBlockSectionItem > <apex:outputLabel for="searchText"> Enter zip code,5 digits, all numbers</apex:outputLabel> <apex:panelGroup > <apex:inputText id="searchText" value="{!searchText}" onchange="return validateText(this.value);" /> <apex:commandButton action="{!doSearch}" value="Search" id="commandbutton"onkeypress="13"/> <script> function validateText(numb){ if(isNaN(parseInt(numb))) {alert("Zip code must be entered in numbers only") return false } else return true }; </script> </apex:panelGroup> </apex:pageBlockSectionItem> </apex:pageBlockSection> <apex:actionStatus id="status" startText="requesting..."/> <apex:pageBlockSection title="Lead Zip Code Search" id="results" columns="1"> <apex:pageBlockTable value="{!results}" var="l" rendered="{!NOT(ISNULL(results))}"> <apex:column value="{!l.postalcode}"/> <apex:column value="{!l.company}"/> <apex:column value="{!l.name}"/> <apex:column value="{!l.email}"/> </apex:pageBlockTable> </apex:pageBlockSection> </apex:pageBlock> </apex:form> </apex:page>
Thanks
Hi Aruna,
I was not able to solve the problem with Apex, and eventually scrapped the project. I got it to the testing phase and the users decided it was easier to use the search functions. All that work! However the only way I was able to control the zip code data was with the following statement in the code:
this way the users know they need to format it a certain way, otherwize the SOQL query will not work correctly. I hope this helps.
Thanks
All Answers
Hi - you can use wildcard search with the LIKE operator in SOQL - I'm surprised you're getting 'random values'. Remember that the % wildcard matches zero or more characters while the _ wildcard matches exactly one character. So if you execute
you should get all Leads whose zip starts with the number '985' (98543, 98562 etc.). If you execute
you should get all Leads whose zip contains the sequence '985' (49854, 56985 etc.).
What are the exact results that you're getting?
Hi, actually now I have an entirely new set of issues. I am getting a Visualforce error:
Read only property '[zipController].searchText' which just started. I have not changed anything related to searchText.
Any clues? I am running my Visualforce page as a custom link on my home page in the sandbox, but it was working fine until this happened.
Thanks,
In the controller class, replace
with
Thanks, that did it. Was it the return command that was confusing?
Hi ,
I do have same problem.
I need to get the zip code 43125-456 this formate.
how to do wildcard search on zip code.
mean i need to get the all number after '-' (456).
can you suggest me on this
Thank you
Hi Aruna,
I was not able to solve the problem with Apex, and eventually scrapped the project. I got it to the testing phase and the users decided it was easier to use the search functions. All that work! However the only way I was able to control the zip code data was with the following statement in the code:
this way the users know they need to format it a certain way, otherwize the SOQL query will not work correctly. I hope this helps.
Thanks