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
Surya KallakuriSurya Kallakuri 

My code is not working..I tried the task like..If I search for an account details.... if the account exists it should display account details or else it should throw me a error message as account not exists

 
visual force:
Code:
<apex:page controller="searchcls" >
  <apex:form >
  <apex:pageBlock >
  <apex:inputText value="{!stringval}"/>
  <apex:commandButton value="search" action="{!search}"/>
  </apex:pageBlock>
  <apex:pageBlock >
  <apex:pageblockTable value="{!acctlst}" var="a">
  <apex:column headerValue="Account Name" value="{!a.name}"/>
  <apex:column headerValue="Fax" value="{!a.Fax}"/>
  <apex:column headerValue="Phone" value="{!a.phone}"/>
  <apex:column headerValue="Billingcountry" value="{!a.billingcountry}"/>
  <apex:column headerValue="Billingstate" value="{!a.billingstate}"/>
  </apex:pageblockTable>
  </apex:pageBlock>
  </apex:form>
</apex:page>

Apex controller:
 public class searchcls {
    public List<Account> acctlst { get; set; }
    public String stringval { get; set; }
    public PageReference search() {
       acctlst = (List<Account>)[FIND: stringval RETURNING Account(name, fax, phone, billingstate, billingcountry)][0];       
         if(acctlst =! null) return 'acctlst';
    if(acctlst == null) return 'unknown';
   }
}
 
Best Answer chosen by Surya Kallakuri
Bryan JamesBryan James
I made some adjustments to what you already had but it really looks like you are on the right foot. 

Visualforce:
<apex:page controller="searchcls" >
  <apex:form >
  <apex:pageBlock >
      
  <apex:inputText value="{!stringval}"/>
  <apex:commandButton value="search" action="{!search}" reRender="account"/><!--I added a rerender attribute so that the page would stop trying to refresh instead it just refreshes the pageblock below-->
  </apex:pageBlock>
     
  <apex:pageBlock id="account"><!--I added an id so that the command button knew what to refresh-->
      <h1 style='color:blue'>
          {!message}<!--This is where the page message will display-->
      </h1>
      
  <apex:pageblockTable value="{!acctlst}" var="a">
  <apex:column headerValue="Account Name" value="{!a.name}"/>
  <apex:column headerValue="Fax" value="{!a.Fax}"/>
  <apex:column headerValue="Phone" value="{!a.phone}"/>
  <apex:column headerValue="Billingcountry" value="{!a.billingcountry}"/>
  <apex:column headerValue="Billingstate" value="{!a.billingstate}"/>
  </apex:pageblockTable>
  </apex:pageBlock>
  </apex:form>
     
</apex:page>

Apex Controller:
public class searchcls {
    public List<Account> acctlst { get; set; }
    public String stringval { get; set; }
    public String message {get;set;}//This is what will be used to display our message
    //I changed your method from a return type to a void that way your page is not trying to redirect
    public void search() {
       acctlst = (List<Account>)[FIND: stringval RETURNING Account(name, fax, phone, billingstate, billingcountry)][0];       
         if(acctlst.size() > 0)//You had the the ! on the wrong side
         {
             //Since I am not trying to return anything Im using our message to let the user know if their search is successful or not.
             message = 'Is this the Account you were looking for?';
             
             
         } else {
        
            //Since I am not trying to return anything Im using our message to let the user know if their search is successful or not.
           
            message = 'No Accounts';
        }
   }
}

Really hope this helps you out.
Happy Holidays!

All Answers

Bryan JamesBryan James
I made some adjustments to what you already had but it really looks like you are on the right foot. 

Visualforce:
<apex:page controller="searchcls" >
  <apex:form >
  <apex:pageBlock >
      
  <apex:inputText value="{!stringval}"/>
  <apex:commandButton value="search" action="{!search}" reRender="account"/><!--I added a rerender attribute so that the page would stop trying to refresh instead it just refreshes the pageblock below-->
  </apex:pageBlock>
     
  <apex:pageBlock id="account"><!--I added an id so that the command button knew what to refresh-->
      <h1 style='color:blue'>
          {!message}<!--This is where the page message will display-->
      </h1>
      
  <apex:pageblockTable value="{!acctlst}" var="a">
  <apex:column headerValue="Account Name" value="{!a.name}"/>
  <apex:column headerValue="Fax" value="{!a.Fax}"/>
  <apex:column headerValue="Phone" value="{!a.phone}"/>
  <apex:column headerValue="Billingcountry" value="{!a.billingcountry}"/>
  <apex:column headerValue="Billingstate" value="{!a.billingstate}"/>
  </apex:pageblockTable>
  </apex:pageBlock>
  </apex:form>
     
</apex:page>

Apex Controller:
public class searchcls {
    public List<Account> acctlst { get; set; }
    public String stringval { get; set; }
    public String message {get;set;}//This is what will be used to display our message
    //I changed your method from a return type to a void that way your page is not trying to redirect
    public void search() {
       acctlst = (List<Account>)[FIND: stringval RETURNING Account(name, fax, phone, billingstate, billingcountry)][0];       
         if(acctlst.size() > 0)//You had the the ! on the wrong side
         {
             //Since I am not trying to return anything Im using our message to let the user know if their search is successful or not.
             message = 'Is this the Account you were looking for?';
             
             
         } else {
        
            //Since I am not trying to return anything Im using our message to let the user know if their search is successful or not.
           
            message = 'No Accounts';
        }
   }
}

Really hope this helps you out.
Happy Holidays!
This was selected as the best answer
Surya KallakuriSurya Kallakuri
It's Working
Thanks for the help.
Merry Christmas & Happy Holidays