function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jneilan22jneilan22 

Custom Error Message

Hello,

 

I currently have a VF page and custom controller.  The page has 5 input values which are then written back to the custom Object I have created.  There are also Save and Cancel buttons.  Everything works fine, except in the case where no values are entered and the user clicks the Save button.  I would like to have a custom message appear telling them that they either need to enter a value or click cancel.  Right now I get a System.DmlException error.  This seems like it would be a fairly simple thing to do but I am having a lot of trouble figuring it out.  Any help would be appreciated!  Below is my code:

 

VF Page

 

<apex:page standardController="Population_Assumption__c" tabStyle="Population_Assumption__c" extensions="CustomOppController"
 sidebar="False" Title="Update Population Assumptions" recordSetVar="opps" id="mupop">
    <apex:form id="muform">
    <style type="text/css">
    .bPageBlock.pbBody { background-color:#0000CD; }
    .format { font-style:italic; font-weight:bold; color:#0000CD; }
    .box { background-color:LightSteelBlue; }
    .button { text-align:center; padding-top:4px; }
    </style>

        <apex:pageBlock title="UPDATE POPULATION ASSUMPTIONS" mode="edit" id="mub1">
             <apex:pageBlockSection columns="2">
             <apex:outputLabel for="EmployeePop" styleClass="format" value="Eligible Employee Population: ">
                <apex:inputText styleClass="box" value="{!NewEEVal}" id="EmployeePop">
                    <apex:actionSupport event="onchange" rerender="muselectedlist"/>
                </apex:inputText></apex:outputLabel><BR></BR>

             <apex:outputLabel for="SpousePop" styleClass="format" value="Eligible Spouse Population: ">
                <apex:inputText styleClass="box" value="{!NewSPVal}" id="SpousePop">
                    <apex:actionSupport event="onchange" rerender="muselectedlist"/>
                </apex:inputText></apex:outputLabel><BR></BR>

            <apex:outputLabel for="DepPop" styleClass="format" value="Eligible Dependents Population: ">
                <apex:inputText styleClass="box" value="{!NewDEPVal}" id="DepPop">
                    <apex:actionSupport event="onchange" rerender="muselectedlist"/>
                </apex:inputText></apex:outputLabel><BR></BR>

            <apex:outputLabel for="SeniorPop" styleClass="format" value="Eligible Seniors Population: ">
                <apex:inputText styleClass="box" value="{!NewSENVal}" id="SeniorPop">
                    <apex:actionSupport event="onchange" rerender="muselectedlist"/>
                </apex:inputText></apex:outputLabel><BR></BR>

            <apex:outputLabel for="OtherPop" styleClass="format" value="Eligible Other Population: ">
                <apex:inputText styleClass="box" value="{!NewOTHVal}" id="OtherPop">
                    <apex:actionSupport event="onchange" rerender="muselectedlist"/>
                </apex:inputText></apex:outputLabel><BR></BR><BR></BR><BR></BR>

             </apex:pageBlockSection>
            <apex:pageBlockButtons location="top">
                <apex:commandButton styleClass="button" style="background:DarkSeaGreen" value="Save" action="{!quicksave}"/>
                <apex:commandButton styleClass="button" style="background:DarkSeaGreen" value="Cancel" action="{!cancel}"/>
            </apex:pageBlockButtons>

         <apex:pageBlockTable value="{!opps}" var="p">
            <apex:column value="{!p.Name}"/>
            <apex:column value="{!p.EE_Eligible_Population__c}"/>
            <apex:column value="{!p.Spouse_Eligible_Population__c}"/>
            <apex:column value="{!p.Dependents_Eligible_Population__c}"/>
            <apex:column value="{!p.Seniors_Eligible_Population__c}"/>
            <apex:column value="{!p.Other_Eligible_Population__c}"/>

        </apex:pageBlockTable>
           
        </apex:pageBlock>

    </apex:form>
</apex:page>

 

 

Controller

 

public class CustomOppController{

    public CustomOppController(ApexPages.StandardSetController controller) {
oppid=System.currentPageReference().getParameters().get('id');
    }

public List<Population_Assumption__c> PopulationInfo{get;set;}
//public List<Opportunity> OppsRecord{get; set;}
public String oppid{get; set;}
Public Decimal NewEEVal{get;set;}
Public Decimal NewSPVal{get;set;}
Public Decimal NewDEPVal{get;set;}
Public Decimal NewSENVal{get;set;}
Public Decimal NewOTHVal{get;set;}
public CustomOppController()

{
//oppid=System.currentPageReference().getParameters().get('id');
//OppsRecord=[Select id from Opportunity where id=:oppid];
//ApexPages.StandardSetController controller
}
//oppid=System.currentPageReference().getParameters().get('id');
public PageReference quicksave()
{
PopulationInfo=new List<Population_Assumption__c>();
   //oppid=System.currentPageReference().getParameters().get('id');
   for(Population_Assumption__c pop :[Select EE_Eligible_Population__c,Spouse_Eligible_Population__c,Dependents_Eligible_Population__c,
   Seniors_Eligible_Population__c,Other_Eligible_Population__c from Population_Assumption__c where Opportunity__c=:oppid])
    {
      IF (NewEEVal != null) {
      pop.EE_Eligible_Population__c = NewEEVal;
      }
      IF (NewSPVal != null) {
      pop.Spouse_Eligible_Population__c = NewSPVal;
      }
      IF (NewDEPVal != null) {
      pop.Dependents_Eligible_Population__c = NewDEPVal;
      }
      IF (NewSENVal != null) {
      pop.Seniors_Eligible_Population__c = NewSENVal;
      }
      IF (NewOTHVal != null) {
      pop.Other_Eligible_Population__c = NewOTHVal;
      }
     
      PopulationInfo.add(pop );
    }

update PopulationInfo;   

PageReference Newpage=new PageReference('/'+oppid);
return NewPage;

}
}

 

Best Answer chosen by Admin (Salesforce Developers) 
kloudrootkloudroot

try something like 

 

if(yourfield = null){

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'my error msg');
2
ApexPages.addMessage(myMsg);
} 

 

 also checkout http://wiki.developerforce.com/index.php/An_Introduction_to_Exception_Handling

All Answers

kloudrootkloudroot

try something like 

 

if(yourfield = null){

ApexPages.Message myMsg = new ApexPages.Message(ApexPages.Severity.FATAL,'my error msg');
2
ApexPages.addMessage(myMsg);
} 

 

 also checkout http://wiki.developerforce.com/index.php/An_Introduction_to_Exception_Handling

This was selected as the best answer
jneilan22jneilan22

Thanks.  The developer guide link helped.