0

This is the controller snippet. Somehow it doesn't work. I have checked the results and it is null, but the error is not showing. I also have apex:messages in my VisualForce page.

public PageReference doSearch() {
    results =  [SELECT Name, Employee_No__c, Email, ESS_User__c, Phone FROM Contact WHERE Employee_No__c =:searchEmpNo AND SSS_No__c =:searchEmpSSSNo];
    if (results == null){
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'No Results Found'));
        }
        return null;
    }
  • 1
    have you add <apex:pageMessages/> tag in VF page? if yes.. you need to rerender this tag when you call doSearch method from any button.. – Ratan Paul Apr 14 '16 at 2:54
  • i have rerender but still, message is not shown @sfdcweb – sfdclearner Apr 14 '16 at 3:12
  • 2
    Results CANNOT be null (so not sure how you verified it). It will either be an empty list or an error will be thrown (List has no rows for assignment to sObject) if no results are found (if results is not a list) – Eric Apr 14 '16 at 3:37
  • try using count() in query, maybe it will work for this scenario – Micz Domz Apr 14 '16 at 5:30
5

If you are using reRender, then the issue is here

if (results == null){

change this to

if (results.isEmpty())

, because query will return empty list.

-1

try this

public PageReference doSearch() {
    Integer counter = 0;
    counter =  [Select count() FROM Contact WHERE Employee_No__c =:searchEmpNo AND SSS_No__c =:searchEmpSSSNo ];
    if(counter == 0){
           ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR, 'No Results Found'));


         }
          return null;
   }

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.