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
Che SFDCChe SFDC 

Help with Validation Rules on VF Page

Dear all, I`m new to apex and I would like to understand how can I add multiple validation rules to this extension. Our org has multiple validation rules and we would like to populate that on VF page. Can someone pls give me an example of how I can use multiple Try/Catch statements in dosave() method below. Whatever I`m trying is failing. Please help!!!

Thanks!
 
public with sharing class ExampleTP {

    String oppId;
    String ownerId;
    private final Opportunity Oppty;
 
    public ExampleTP (ApexPages.StandardController stdController) {
        oppId = stdController.getId();
        Oppty = (Opportunity) stdController.getRecord();
        ownerId = oppty.OwnerId;
        if(oppty.StageName == null)
          oppty.StageName = 'Identified Opportunity';
        if(oppty.Probability == null)
            oppty.Probability = 0;
    }



public transient Map<String, Decimal> probabilityStageNameMap;

public PageReference changeStageName() {

if (probabilityStageNameMap == null) {
 probabilityStageNameMap = new Map<String, Decimal>();
for (OpportunityStage oppStage : [Select MasterLabel, DefaultProbability
                                    From OpportunityStage]) {
 probabilityStageNameMap.put(oppStage.MasterLabel, oppStage.DefaultProbability);
   }
  }

 if (probabilityStageNameMap.containsKey(Oppty.StageName)) {
   Oppty.Probability = probabilityStageNameMap.get(Oppty.StageName);
 
 }

  return null;
 }
 
  public PageReference doSave()
  {
  Try
   {
      if (Oppty.LeadSource == 'Marketing Campaign' && Oppty.CampaignId == null )
  
  {
    upsert oppty;
     return new PageReference('/' + 'apex/TPDynamicOppNEWLayout?oppId= + Oppty.Id'); 
        }
        }
catch(Exception e)
        {
            Apexpages.addMessage(new Apexpages.message(ApexPages.Severity.Error,'Campaign is required'));
            }
            return null;
  }
        


}

 
Best Answer chosen by Che SFDC
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Chetan Jomiwale,

If a user enters data on a Visualforce page that uses a custom controller, and that data causes a validation rule error, the error can be displayed on the Visualforce page. Like a page that uses a standard controller, if the validation rule error location is a field associated with an<apex:inputField> component, the error displays there. If the validation rule error location is set to the top of the page, use the <apex:messages> component within the <apex:page> to display the error. However, to get the information to the page, the custom controller must catch the exception.

You can always use try/catch block as below.
try{
 YOUR CODE

}
catch(Exception ex){
ApexPages.addMessages(ex);
}

Let us know if it helps you.

All Answers

Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Chetan Jomiwale,

If a user enters data on a Visualforce page that uses a custom controller, and that data causes a validation rule error, the error can be displayed on the Visualforce page. Like a page that uses a standard controller, if the validation rule error location is a field associated with an<apex:inputField> component, the error displays there. If the validation rule error location is set to the top of the page, use the <apex:messages> component within the <apex:page> to display the error. However, to get the information to the page, the custom controller must catch the exception.

You can always use try/catch block as below.
try{
 YOUR CODE

}
catch(Exception ex){
ApexPages.addMessages(ex);
}

Let us know if it helps you.
This was selected as the best answer
Che SFDCChe SFDC
Asish, you are brilliant! After adding above code to my class, it now catches all the errors on !dosave () and standard save operations. Thank you so much for your kind reply. I hope you have a good weekend!