0

class code

public class democlass {
 public string searchboxstring{get; set;}
 public list<contact> result {get; set;}
 public Integer counter{get;set;}

 public democlass(){
  //result = new list<Contact>();
 }
 public void searchbox(){
  result=new List<Contact>();
  if(searchBoxstring.trim() != null || searchBoxstring.trim() != ''){
      result=[select Name, account.name from Contact where account.Name =:searchboxstring];
      counter = result.size();
  }
  else{
      ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'please enter Account Name');
      ApexPages.addMessage(myMsg);
  }
 }
}

this is page code

<apex:page controller="democlass">  
<apex:form >
<apex:pageBlock >
<apex:pageMessages/>
 <apex:pageBlockSection columns="2">
 <apex:inputText value="{!searchBoxstring}" label="Search"/>
   <apex:commandButton value="Search" action="{!searchbox}" >
    <apex:commandButton value="Add" onclick="window.open=('/003','_blank');"/>
  </apex:commandButton>

    <apex:pageblockTable value="{!result}" var="wl">
     <apex:column title="Name">
          <apex:outputLink value="/{!wl.id}" target="_blank">{!wl.name}</apex:outputLink>
     </apex:column>
      <apex:column value="{!wl.account.name}"/>
      <apex:column value="{!wl.id}"/>
    </apex:pageblockTable>
    <br/><br/>
    <apex:outputText>Total Record : {!counter} </apex:outputText>   
  </apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page> 
1

You need to update your if condition

if(searchBoxstring.trim() != null && searchBoxstring.trim() != ''){
      result=[select Name, account.name from Contact where account.Name =:searchboxstring];
      counter = result.size();
  }
  else{
      ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'please enter Account Name');
      ApexPages.addMessage(myMsg);
  }

because if its null then it will not equal to empty string and your condition will be satisfied.

| improve this answer | |
0

I feel couple of corrections in apex class can make it work

public class democlass {
 public string searchboxstring{get; set;}
 public list<contact> result {get; set;}
 public Integer counter{get;set;}

 public democlass(){
  //result = new list<Contact>();
 }
 public Pagereference searchbox(){
  result=new List<Contact>();
  if(searchBoxstring != null && searchBoxstring.trim() != ''){
      result=[select Name, account.name from Contact where account.Name =:searchboxstring];
      counter = result.size();
  }
  else{
      ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.ERROR,'please enter Account Name');
      ApexPages.addMessage(myMsg);
  }
 return null;
}
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.