1

I have created a custom object and replicated the same using Visual force, and also have written a validation rule saying "Source should not be equal to destination". But when i am clicking on save its not throwing the error instead it is redirecting me to the new page whose link i have given in the controller class. Please suggest me so that I can get the validation error message. PFB the code snippets.

Below is the code for the Visualforce page and the controller class

<apex:page standardController="Ticket_Booking__c" extensions="SaveAndNavigate">
<apex:form >
  <apex:pageBlock Title="Book Your Ticket">
  <apex:pageBlockSection Title="Journey Information">
  <apex:inputField value="{!Ticket_Booking__c.Source__c}"/>
  <apex:inputField value="{!Ticket_Booking__c.Destination__c}"/>
  <apex:inputField value="{!Ticket_Booking__c.Date_of_Journey__c}"/>
  </apex:pageBlockSection>
  <apex:pageBlockSection Title="Passenger Information">
  <apex:inputField value="{!Ticket_Booking__c.Passenger_Information__c}"/>
  </apex:pageBlockSection>
  <apex:pageBlockButtons >
  <apex:commandButton value="Save" action="{!Navigate}"/>
  <apex:commandButton value="Cancel" action="{!Cancel}"/>
  </apex:pageBlockButtons>
  </apex:pageBlock>
  </apex:form>
</apex:page>

Class

public class SaveAndNavigate {
public Ticket_Booking__c ticket{get; set;}
    public String pnrNo{get; set;}
    private ApexPages.StandardController controller;
    public SaveAndNavigate(ApexPages.StandardController controller) {
this.controller = controller;
}
public PageReference Navigate() {
  controller.save(); // This takes care of the details for you.
  ticket=(Ticket_Booking__c)controller.getRecord();
  pnrNo=ticket.source__c.substring(0,3).touppercase()+'-'+ticket.Destination__c.Substring(0,3).touppercase()+'-'+ticket.Date_of_journey__c.year()+ticket.Date_of_journey__c.day();
  system.debug(pnrNo);
  PageReference p= new PageReference('/apex/GeneratePNRDetails?att='+pnrNo);
 // p.setRedirect(true);
  return p;
}

}
  • Can you share the logic for your validation rule and see if it is active ? – Dave Jun 5 '17 at 17:31
1

controller.save(); will not throw any Exception in case of validation rule errors or trigger validations. It just adds the error message to the <apex:pageMessages/> and returns null.

So check the returned value of this method to control your flow.

if(controller.save() == null)
{
  return null;//end of execution. You see your errors in the page.
}
ticket=(Ticket_Booking__c)controller.getRecord();
...
return p;

controller.save() will return a valid PageReference instance if its a successful save, so it will not enter the if block in that case.

And of course, add <apex:pageMessages/> in the visualforce page.

I have faced this issue previously and learnt about this behavior in a good answer by Bob Buzzard somewhere.

1

First add <apex:pageMessages/> in the visualforce page.

Add try-catch block to capture exception throw it to the page.

Update:

I will suggest you to verify size of List<Apexpages.Message> just after controller.save() . If size is greater than 0 then return null.

public PageReference Navigate() {
  try
  {
    List<Apexpages.Message> msgs;
    controller.save();
    msgs = ApexPages.getMessages();
    if(msgs.size()>0)
    {
        return null;
    }

    ticket=(Ticket_Booking__c)controller.getRecord();
    pnrNo=ticket.source__c.substring(0,3).touppercase()+'-'+ticket.Destination__c.Substring(0,3).touppercase()+'-'+ticket.Date_of_journey__c.year()+ticket.Date_of_journey__c.day();
    system.debug(pnrNo);
    PageReference p= new PageReference('/apex/GeneratePNRDetails?att='+pnrNo);
    // p.setRedirect(true);
    return p;
  }catch(Exception ex)
  {
      ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error, ex.getMessage());
      ApexPages.addMessage(msg);
      return null;
  }

} 
  • Santanu, unless the user compares the source and destination and return an error message, adding a catch block would not how the message. – Dave Jun 5 '17 at 17:35

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.