0

I am trying to incorporate bottom logic here.

If mrn is not null, trigger "guard clause" saying 'if length of FirstName is less than 3, "Please specify.." and if length of LastName is less than 3, "Please specify..".

I am struggling regards to If statement logic as well as how to express "reRender" correctly on apex:commandButton as well as applying into apex:pageBlockSection (id="msgs?) on the bottom after commandButton.

I think logic got mixed up with mrn, lname and fname.

Here is my updated Apex and VF code:

public with sharing class psw {
    public String sfprn { get; set; }
    public String mrn {get;set;}
    public String lname {get;set;}
    public String fname {get;set;}


public class TableRow2{
    public String sfprn {get;set;}
    public String mrn {get;set;}
    public String lname {get;set;}
    public String fname {get;set;}
    public Decimal SurveySelection {get;set;}
}

public List<TableRow2> RowList {get; set;}

public PageReference searchPatients(){

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

    if (mrn == null )
    { 
        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, 
                                mrn__c,
                                LName__c, 
                                FName__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;

        RowList.add(tr);         

        }
     return null;   
} 

}

<apex:page Controller="psw" showHeader="false" sidebar="false">
<apex:form >
<apex:pageBlock >
    <!-- Search button-->
    <apex:pageBlockButtons location="top">
        <!-- Added 5/30 -->   
        <apex:pageMessages id="msgs"></apex:pageMessages> 

        <!-- Modified 5/30 -->
        <!--<apex:commandButton value="Search" action="{!searchPatients}" reRender="contact-table"/>  -->
        <apex:commandButton value="Search" action="{!searchPatients}" reRender="msgs"/>
    </apex:pageBlockButtons>

    <!-- Modified 5/30 -->
    <!--        
    <apex:pageBlockSection id="contact-table" columns="1">  -->
    <apex:pageBlockSection id="msgs" columns="1">
        <!-- Input starts -->
        <apex:pageBlockSectionItem >
            <apex:outputLabel value="MRN" />
            <apex:inputText value="{!mrn}"/>            
        </apex:pageBlockSectionItem>

        <apex:pageBlockSectionItem >
            <apex:outputLabel value="Last Name" />
            <apex:inputText value="{!lname}"/>            
        </apex:pageBlockSectionItem>
        <apex:pageBlockSectionItem >
            <apex:outputLabel value="First Name" />
            <apex:inputText value="{!fname}"/>            
        </apex:pageBlockSectionItem> 
        <!-- Input ends -->

        <!-- Output 1 starts --->
        <apex:pageBlockTable value="{!RowList}" var="c">
            <apex:column >
                <apex:facet name="header">MRN</apex:facet>
                {!c.mrn}
            </apex:column>
            <apex:column >
                <apex:facet name="header">Last Name</apex:facet>
                {!c.lname}
            </apex:column>
            <apex:column >
                <apex:facet name="header">First Name</apex:facet>
                {!c.fname}
            </apex:column>                         
         </apex:pageBlockTable>
         <!-- Output 1 ends --->  
     </apex:pageBlockSection>       
    </apex:pageBlock>    
</apex:form>    
</apex:page>
0

2 Answers 2

4

If you want to do it on the controller side, you would use ApexPages.addMessage. You'll need to add an early return in this case (called a "guard clause") so that you don't process the rest of your logic. This sort of validation would look roughly like:

if (fname != null || fname.length() < 3)
{
    ApexPages.addMessage(new ApexPages.Message(
        ApexPages.Severity.ERROR, 'Please specify at least three characters'
    ));
    return null;
}
// same for last name

You'll need to rerender an <apex:pageMessages> element in order to display these errors. Your markup changes would look roughly like:

<apex:page ...>

    <apex:pageMessages id="msgs" />

    <!-- intermediate markup -->

    <apex:commandButton ... rerender="msgs" />

    <!-- other markup -->

</apex:page>
1
  • This is good stuff. Thanks. I was able to add that, but I am getting logical error with "IF" statement of other variable (mrn). How do I incorporate this into VF as id points to "msgs"? I updated my codes.
    – Java
    May 30, 2017 at 17:22
4

You'll want to check if the length is less than 3 as well:

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .