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
SunadminSunadmin 

Creating no results message and zip code error for visual force page and apex class

I am working on an Apex class and visual force page that will search lead zip codes and provide the company, zip code, contact name, and e-mail when the search is complete. I based it on a class and page I found in the force.com community. I was able to get the class and page working correctly, but there are a couple of modifications I tried to make.

The first was to give a message when there were no search results.  The second was to give an error message when the zip code is entered incorrectly. I based this one on the zip code format validation rule. Here is the Apex class, with my attempt to insert a “no results” message. I tried using a “get,set” with an “if message:

 

public class zipController {
    public String searchResults { get; set; }
    public String displayResults { get; set; }
    String searchText;
    List<Lead> results;
    public String getSearchText() {
        return searchText;
    }
    public void setSearchText(String s) {
        searchText = s;
    }
    public List<Lead> getResults() {
        return results;
    }
public PageReference doSearch() {
    results = (List<Lead>)[FIND :searchText RETURNING Lead(Company, Name, Email, PostalCode)][0];
    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 current selected criteria'));  
          }
          else
          {
            apexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO, 'Here are your search results'));

          }

    return null;
    }
}

 

 

I know I am using a numerical value for a string [if (results.size() == 0)] but I am not getting an error when I compile.

 

 

Here is my visual force page:

<apex:page controller="zipController">
    <apex:form >
        <apex:pageBlock mode="edit" id="block">
        <apex:pageBlockSection >
        <apex:pageBlockSectionItem >
    <apex:outputLabel for="searchText"> Enter zip code</apex:outputLabel>
        <apex:panelGroup >
        <apex:inputText id="searchText" value="{!searchText}"/>
        <apex:commandButton value="Go!" action="{!doSearch}"
            rerender="block" status="status"/>
        </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>

 

On the visual force page, I also tried a “reRendered” command in two different methods. I put these right after the rendered="{!NOT(ISNULL(results))}">

<apex:outputPanel layout="block" rendered="{!AND(displayResults, searchResults.size=0)}">Sorry, your search returned no results.</apex:outputPanel>                       

                    

reRendered="{!NOT(ISNULL(results))}">

            if"{!results}" var="l"

            rendered="{ISNULL(results)}"

            <apex:Message>"Search Returned No Results"</apex:Message>

For the zip code error, here is the validation rule:

AND(

OR(BillingCountry = "USA", BillingCountry = "US"),

NOT(REGEX( BillingPostalCode , "\\d{5}(-\\d{4})?"))

)

 

Error Message: Zip code must be in 99999 or 99999-9999 format.

Error Location: Billing Zip/Postal Code

 

Here is the insert for the visual force page:

<font color="red"><apex:messages styleClass="error" /> </font>

I know I am missing something on this. I also thought about using a “try-throw” with an “if”:

try (

integer i;

            if {(i>5) OR (i<5)} throw new MyException();

            { catch (MyException e)} “error message”

 I would need to set the lead.PostCode string as an integer first NUM(lead.PostalCode)

 

I would appreciate it if someone could take a look and see what I am doing wrong on both of these. For the “no results” in the Apex class I am not getting any errors when I compile, but it does not do anything. For the “incorrect zip code” I cant figure out how to insert the validation rule or possibly how to create it on the page.

 

Thanks,