I am not sure why these codes (where Guard Clauses (for FirstName and LastName) are) do not show the result of data on the VF page.

Am I missing something on VF page or is the order of logic causing the issue?

public PageReference searchPatients(){

    RowList = new List<TableRow2>();
    TableRow2 tr;             

    if (lname != null && lname.length() < 3)
        ApexPages.addMessage(new ApexPages.Message(
        ApexPages.Severity.ERROR, 'Please specify at least three characters of Last Name'
        return null;

    if (fname != null && fname.length() < 3)
        ApexPages.addMessage(new ApexPages.Message(
        ApexPages.Severity.ERROR, 'Please specify at least three characters of First Name'
        return null;

    String bind_fname = string.isBlank(fName) || fname.length() <3 ? null : (fname.left(3) + '%');
    String bind_lname = string.isBlank(lname) || lname.length() <3 ? null : (lname.left(3) + '%');

    for(Patient__c con : [SELECT sfprn__c, 
                                (select SurveySelection__c.SurveySelection__c from SurveySelections__r) 
                                FROM Patient__c
                                where ( mrn__c =:mrn )                                                           
                                or ( LName__c LIKE :bind_lname and FName__c LIKE :bind_fname ) LIMIT 1 
        tr = new TableRow2();
        tr.sfprn = con.sfprn__c;
        tr.mrn = con.mrn__c;
        tr.lname = con.LName__c;
        tr.fname = con.FName__c;
        tr.SurveySelection = con.SurveySelections__r.isEmpty() ? null : con.SurveySelections__r[0].SurveySelection__c;

        return null;   

Here is VF markup.

<apex:page Controller="psw" showHeader="false" sidebar="false">
<apex:form >
<apex:pageBlock >

    <apex:pageBlockButtons location="top">
        <apex:pageMessages id="msgs" />       

        <apex:commandButton value="Search" action="{!searchPatients}" rerender="msgs"/> 

    <!-- Other stuff -->

 </apex:pageBlock >               
</apex:form >

  • By the way, if you're returning early, the ternary operators that were introduced in a previous question are no longer necessary... – sfdcfox May 30 '17 at 20:16
  • As an aide when asking a question, avoid "does not work" or "gives an error" as it is way to vague. Please be specific and describe what is happening including any error message verbatim. The more detailed you are the better answers you will get. Also, ensure you are only posting relevant code...I have edited a an example for the question – Eric May 30 '17 at 20:25
  • Thanks Eric. That is very clever. I did not know whether some of VF mark up was related or not. sfdcfox, I am not sure what you meant by "ternary operators". – Java May 30 '17 at 20:31
  • @KingJava - Review your question: salesforce.stackexchange.com/questions/176483/… on this. The answer I gave hows a ternary operator. And yea, when you don't know all the code is good. I was jut showing an example. Finding what i not needed i hard sometimes especially when you do not know what the issue is. By no means am I saying not to post as much code as you want just make sure that the actual code needed does not get buried in a sea of characters – Eric May 30 '17 at 20:40

It should be an or condition:

if (lname == null || lname.length() < 3)

In plain English, "if lname is empty, or lname's length is less than 3, then..."

  • @KingJava You didn't add an <apex:pageMessages id="msgs" /> tag to enable the UI to surface the error. – Adrian Larson May 30 '17 at 20:40
  • I have it on the UI (as shown above). Is something missing? – Java May 30 '17 at 20:46
  • sfdcfox, actually, your code shows an error message. It was my bad. I did not change from && to ==. But the issue now is that after it passes all the error messages, still it would not reflect any output. – Java May 30 '17 at 21:02
  • @KingJava You'll need to check your query results. Everything looks okay. Make sure the data's correct. – sfdcfox May 30 '17 at 21:05
  • Hmm.. Not sure what is going on.. Wouldn't it be somewhere in VF page or on SOQL possibly? – Java May 30 '17 at 21:16

I found the solution. It was due to location of "apex:pageMessages" was in VF page. I had to move "apex:pageMessages id="msgs"/
from inside "apex:pageBlockButtons" to "apex:pageBlockSection".

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.