+ Start a Discussion
Steven HoughtalenSteven Houghtalen 

I need some help with putting an error message on a visualforce page.

I have created three pages/code that the user sequences through and it all works fine.  On the second page, I have added some error checking code and want to display an error message and allow the user to fix the entry.  If there is no error, I want to continue on to the third page.  I can't seem to find a way to make it display the error message and stay on the same page.  Hopefully someone can help.

Here is a partial copy of the code where the error checking takes place along with the 2nd visualforce page.

       If (cpin == ClientRegisteredPin){
             
            for(Meds__c m :Meds) {
                Med_Log__c mlrec = new Med_Log__c();
                If (m.Dose_Taken__c <> null) {
                    mlrec.Dose_Taken__c = m.Dose_Taken__c;
                    mlrec.Count__c = m.Count__c;
                    mlrec.Client_Pin__c = cpin;
                    mlrec.As_Prescribed__c = m.As_Prescribed__c;
                    mlrec.Name_of_Med__c = m.Name_of_Med__c;
                    mlrec.Active__c = m.Active__c;
                    mlrec.Client__c = m.Client__c;
                    mlrec.Dosage__c = m.Dosage__c;
                    mlrec.Frequency__c = m.Frequency__c;
                    mlrec.Controlled_Drug__c = m.Controlled_Drug__c;
                    mlrec.Count_In__c = m.Count_In__c;
                    mlrec.Count_Out__c = m.Count_Out__c;
                    mlrec.Date__c = datetime.now();
                    mlrec.Staff__c = staffname;
                    MedsLog.add(mlrec);
                }
            }

            
            Insert MedsLog;
            
            for(Meds__c m :Meds) {
                m.Dose_Taken__c = null;
                m.Count__c = null;
                m.Client_Pin__c = null;
                m.As_Prescribed__c = false;
                m.Date__c = null;
                m.Count_In__c = null;
                m.Count_Out__c = null;
                
            }
            Update Meds;
            
    
            PageReference MedsPage = Page.MedSelClient;
            MedsPage.setRedirect(true);
            return MedsPage;
        }
        Else{
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Invalid Pin - re-enter or enter Master Pin'));
            PageReference MedsPage = Page.LogMeds;
            MedsPage.setRedirect(false);
            return MedsPage;
            
        }  

<apex:page Controller="LogMeds"  sidebar="false">
    <apex:pageBlock title="Medication Record for {!clientname}"/>
   <apex:form >
    <apex:pageBlock >
    <apex:pageMessages id="showmsg"></apex:pageMessages>
          <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!save}" rerender="showmsg"/>
          </apex:pageBlockButtons>
          <b>Select witnessing staff member</b> &nbsp;  
         <apex:InputField value="{!clo.Staff__c}" required="true" style="width:100px"/>
         <b>Enter Client Pin</b> &nbsp;
         <apex:InputField value="{!clo.Client_PIN__c}" required="true" style="width:100px"/>
        
          <apex:pageBlockTable value="{!Meds}" var="ml">
            <apex:column value="{!ml.Name_of_Med__c}"/>
            <apex:column value="{!ml.Controlled_Drug__c}"/>
            <apex:column value="{!ml.Expiration_Date__c}"/>                 
            <apex:column value="{!ml.Dosage__c}"/> 
            <apex:column value="{!ml.Frequency__c}"/>
            <apex:column headerValue="Dose Taken">
                <apex:InputField value="{!ml.Dose_Taken__c}"  style="width:100px"/>
            </apex:column>
           
            <apex:column headerValue="Taken as Prescribed" >
                <apex:InputField value="{!ml.As_Prescribed__c}"/>
            </apex:column>
            <apex:column headerValue="Count Out">
                <apex:InputField value="{!ml.Count_Out__c}" style="width:100px"/>
            </apex:column>
            <apex:column headerValue="Count In">
                <apex:InputField value="{!ml.Count_In__c}" style="width:100px"/>
            </apex:column>
          </apex:pageBlockTable>
    </apex:pageBlock>
 </apex:form>
 </apex:page>
                               
 
Best Answer chosen by Steven Houghtalen
Waqar Hussain SFWaqar Hussain SF
If (cpin == ClientRegisteredPin){
             
            for(Meds__c m :Meds) {
                Med_Log__c mlrec = new Med_Log__c();
                If (m.Dose_Taken__c <> null) {
                    mlrec.Dose_Taken__c = m.Dose_Taken__c;
                    mlrec.Count__c = m.Count__c;
                    mlrec.Client_Pin__c = cpin;
                    mlrec.As_Prescribed__c = m.As_Prescribed__c;
                    mlrec.Name_of_Med__c = m.Name_of_Med__c;
                    mlrec.Active__c = m.Active__c;
                    mlrec.Client__c = m.Client__c;
                    mlrec.Dosage__c = m.Dosage__c;
                    mlrec.Frequency__c = m.Frequency__c;
                    mlrec.Controlled_Drug__c = m.Controlled_Drug__c;
                    mlrec.Count_In__c = m.Count_In__c;
                    mlrec.Count_Out__c = m.Count_Out__c;
                    mlrec.Date__c = datetime.now();
                    mlrec.Staff__c = staffname;
                    MedsLog.add(mlrec);
                }
            }

            
            Insert MedsLog;
            
            for(Meds__c m :Meds) {
                m.Dose_Taken__c = null;
                m.Count__c = null;
                m.Client_Pin__c = null;
                m.As_Prescribed__c = false;
                m.Date__c = null;
                m.Count_In__c = null;
                m.Count_Out__c = null;
                
            }
            Update Meds;
            
    
            PageReference MedsPage = Page.MedSelClient;
            MedsPage.setRedirect(true);
            return MedsPage;
        }
        Else{
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Invalid Pin - re-enter or enter Master Pin'));
            return null;
            
        }


 
<apex:page Controller="LogMeds"  sidebar="false">
    <apex:pageBlock title="Medication Record for {!clientname}"/>
   <apex:form >
    <apex:pageBlock id="pb">
    <apex:pageMessages id="showmsg"></apex:pageMessages>
          <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!save}" rerender="showmsg,pb"/>
          </apex:pageBlockButtons>
          <b>Select witnessing staff member</b> &nbsp;   
         <apex:InputField value="{!clo.Staff__c}" required="true" style="width:100px"/>
         <b>Enter Client Pin</b> &nbsp;
         <apex:InputField value="{!clo.Client_PIN__c}" required="true" style="width:100px"/>
        
          <apex:pageBlockTable value="{!Meds}" var="ml"> 
            <apex:column value="{!ml.Name_of_Med__c}"/> 
            <apex:column value="{!ml.Controlled_Drug__c}"/>
            <apex:column value="{!ml.Expiration_Date__c}"/>                  
            <apex:column value="{!ml.Dosage__c}"/>  
            <apex:column value="{!ml.Frequency__c}"/>
            <apex:column headerValue="Dose Taken">
                <apex:InputField value="{!ml.Dose_Taken__c}"  style="width:100px"/>
            </apex:column> 
           
            <apex:column headerValue="Taken as Prescribed" >
                <apex:InputField value="{!ml.As_Prescribed__c}"/>
            </apex:column> 
            <apex:column headerValue="Count Out">
                <apex:InputField value="{!ml.Count_Out__c}" style="width:100px"/>
            </apex:column> 
            <apex:column headerValue="Count In">
                <apex:InputField value="{!ml.Count_In__c}" style="width:100px"/>
            </apex:column> 
          </apex:pageBlockTable>
    </apex:pageBlock>
 </apex:form>
 </apex:page>

try above code snippet

All Answers

Waqar Hussain SFWaqar Hussain SF
If (cpin == ClientRegisteredPin){
             
            for(Meds__c m :Meds) {
                Med_Log__c mlrec = new Med_Log__c();
                If (m.Dose_Taken__c <> null) {
                    mlrec.Dose_Taken__c = m.Dose_Taken__c;
                    mlrec.Count__c = m.Count__c;
                    mlrec.Client_Pin__c = cpin;
                    mlrec.As_Prescribed__c = m.As_Prescribed__c;
                    mlrec.Name_of_Med__c = m.Name_of_Med__c;
                    mlrec.Active__c = m.Active__c;
                    mlrec.Client__c = m.Client__c;
                    mlrec.Dosage__c = m.Dosage__c;
                    mlrec.Frequency__c = m.Frequency__c;
                    mlrec.Controlled_Drug__c = m.Controlled_Drug__c;
                    mlrec.Count_In__c = m.Count_In__c;
                    mlrec.Count_Out__c = m.Count_Out__c;
                    mlrec.Date__c = datetime.now();
                    mlrec.Staff__c = staffname;
                    MedsLog.add(mlrec);
                }
            }

            
            Insert MedsLog;
            
            for(Meds__c m :Meds) {
                m.Dose_Taken__c = null;
                m.Count__c = null;
                m.Client_Pin__c = null;
                m.As_Prescribed__c = false;
                m.Date__c = null;
                m.Count_In__c = null;
                m.Count_Out__c = null;
                
            }
            Update Meds;
            
    
            PageReference MedsPage = Page.MedSelClient;
            MedsPage.setRedirect(true);
            return MedsPage;
        }
        Else{
            ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Invalid Pin - re-enter or enter Master Pin'));
            return null;
            
        }


 
<apex:page Controller="LogMeds"  sidebar="false">
    <apex:pageBlock title="Medication Record for {!clientname}"/>
   <apex:form >
    <apex:pageBlock id="pb">
    <apex:pageMessages id="showmsg"></apex:pageMessages>
          <apex:pageBlockButtons >
            <apex:commandButton value="Save" action="{!save}" rerender="showmsg,pb"/>
          </apex:pageBlockButtons>
          <b>Select witnessing staff member</b> &nbsp;   
         <apex:InputField value="{!clo.Staff__c}" required="true" style="width:100px"/>
         <b>Enter Client Pin</b> &nbsp;
         <apex:InputField value="{!clo.Client_PIN__c}" required="true" style="width:100px"/>
        
          <apex:pageBlockTable value="{!Meds}" var="ml"> 
            <apex:column value="{!ml.Name_of_Med__c}"/> 
            <apex:column value="{!ml.Controlled_Drug__c}"/>
            <apex:column value="{!ml.Expiration_Date__c}"/>                  
            <apex:column value="{!ml.Dosage__c}"/>  
            <apex:column value="{!ml.Frequency__c}"/>
            <apex:column headerValue="Dose Taken">
                <apex:InputField value="{!ml.Dose_Taken__c}"  style="width:100px"/>
            </apex:column> 
           
            <apex:column headerValue="Taken as Prescribed" >
                <apex:InputField value="{!ml.As_Prescribed__c}"/>
            </apex:column> 
            <apex:column headerValue="Count Out">
                <apex:InputField value="{!ml.Count_Out__c}" style="width:100px"/>
            </apex:column> 
            <apex:column headerValue="Count In">
                <apex:InputField value="{!ml.Count_In__c}" style="width:100px"/>
            </apex:column> 
          </apex:pageBlockTable>
    </apex:pageBlock>
 </apex:form>
 </apex:page>

try above code snippet
This was selected as the best answer
Steven HoughtalenSteven Houghtalen
Thank you Waqar!!  It worked perfectly.  Much appreciated.

Steve 
Waqar Hussain SFWaqar Hussain SF
Hello Steve, I'm glad I was of help. Please don't forget to mark best answers for the most helpful solution.