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
ManidipaManidipa 

Not getting the inbuilt validation rule error message on the VF page after cloning a record

Hi

I have created an object called travel request and created 2 validation rules on that object.I am using one VF page and 1 controller for it.When we are creating 1 record the validations are working fine and we are getting the error messages on the VF page,but when we are cloning the record the error messages are not showing on the page,though the validation rules are working because it is not saving the record and it is styaing in the same page.We cannot write custom validations on the apex class because the requirement is to use the standard validations strictly.Please help,it is  really urgent ..I am posting the VF code and the apex class code below:

 

The VF page

 

<apex:page standardController="Travel_Request__c" id="page1"  extensions="TravelRequest_SaveExt">
    <apex:form >
   
          <apex:pageBlock title="Travel Request Edit" mode="edit" id="pb1">
            <apex:pageBlockButtons location="top">
                <apex:commandButton action="{!save}" value="Save"/>
                <apex:commandButton action="{!cancel}" value="Cancel"/>
            </apex:pageBlockButtons>
            <apex:pageBlockButtons location="bottom">
                <apex:commandButton action="{!save}" value="Save"/>
                <apex:commandButton action="{!cancel}" value="Cancel"/>
            </apex:pageBlockButtons>
             <apex:pageBlockSection title="Request Details" columns="2">
                <apex:inputField value="{!TRInfo.Travel_Type__c}" required="true"/>
                <apex:inputField value="{!TRInfo.Traveling_To_Remote_Office__c}" required="true"/>
                <apex:inputField value="{!TRInfo.Reason_Code__c}" required="true"/>
                <apex:inputField value="{!TRInfo.Travel_Legs__c}" required="true"/>
            </apex:pageBlockSection>
                <apex:pageBlockSection title="Traveler's Details" columns="2" id="pgs1">
                  <apex:inputField id="textFld" value="{!TRInfo.Traveler_s_Name__c}" required="true"/>
                <apex:inputField value="{!TRInfo.Departure_Date__c}" required="true"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Other Details" columns="2">
                <apex:inputField value="{!TRInfo.NVIDIA_Employees_Traveling_with_You__c}" required="true"/>
                <apex:inputField value="{!TRInfo.Trip_Reason__c}" required="true"/>
                <apex:inputField value="{!TRInfo.Comments__c}"/>
                <apex:inputField value="{!TRInfo.Reason_for_Late_Booking__c}"/>
            </apex:pageBlockSection>
            <apex:pageBlockSection title="Itinerary Details" columns="1">
                <apex:inputField value="{!TRInfo.Total_Air_Fare__c}" required="true"/>
                <apex:outputField value="{!TRInfo.Air_Fare_Currency__c}"/><br/>
                Please enter the rate in USD using the following <apex:outputLink value="http://www.xe.com/" id="theLink" target="blank"><b>Currency Convertor</b></apex:outputLink>
            </apex:pageBlockSection>
        </apex:pageBlock>
         
    </apex:form>
</apex:page>

 

The apex class:

 

public class TravelRequest_SaveExt {
    
    public Travel_Request__c TRInfo {get;set;}
    public boolean showErrors{get;set;}
    String strId;
    public TravelRequest_SaveExt(ApexPages.StandardController controller) {
        TRInfo = new Travel_Request__c();
        TRInfo.Traveler_s_Name__c=userinfo.getUserId();
        system.debug('+++ID+++++'+ApexPages.currentPage().getParameters().get('Id'));
        try{
        if(ApexPages.currentPage().getParameters().get('Id') != null)
        {
            strId = ApexPages.currentPage().getParameters().get('Id');
            TRInfo = null;
            TRInfo = [Select Travel_Type__c,Traveling_To_Remote_Office__c,Reason_Code__c,Travel_Legs__c,
            Traveler_s_Name__c,Departure_Date__c,NVIDIA_Employees_Traveling_with_You__c,Trip_Reason__c,
               Comments__c,Reason_for_Late_Booking__c,Total_Air_Fare__c,Air_Fare_Currency__c From Travel_Request__c Where Id = :strId];
         }
       }
       catch(Dmlexception e){
       }  
    }
 
    public  pageReference save()
    {
        Database.UpsertResult resultObj = null;
        Database.SaveResult resObj = null;
        String clone = ApexPages.currentPage().getParameters().get('clone');
        PageReference pageRef;
    
        if(clone!=null && clone!='' && clone=='1')
        {
          Travel_Request__c TRInfo1= createTravelRequest(TRInfo);
         
           
            resObj=database.insert(TRInfo1);
            System.debug('Info from TravelRequest Object'+TRInfo1);
            pageRef=new PageReference('/'+resObj.getId());
           
            }
        
        else
        {
            try{
            resultObj=database.upsert(TRInfo);  
            pageRef=new PageReference('/'+resultObj.getId());
            }catch(exception e){
            }   
         }
            return pageRef;
     }
    public Travel_Request__c createTravelRequest(Travel_Request__c TRInfo)
    {
 
    Travel_Request__c TRInfo1 = new Travel_Request__c();
   
        TRInfo1.Travel_Type__c= TRInfo.Travel_Type__c;
        TRInfo1.Traveling_To_Remote_Office__c=TRInfo.Traveling_To_Remote_Office__c;
        TRInfo1.Reason_Code__c=TRInfo.Reason_Code__c;
        TRInfo1.Travel_Legs__c=TRInfo.Travel_Legs__c;
        TRInfo1.Traveler_s_Name__c=TRInfo.Traveler_s_Name__c;
        TRInfo1.Departure_Date__c=TRInfo.Departure_Date__c;
        TRInfo1.NVIDIA_Employees_Traveling_with_You__c=TRInfo.NVIDIA_Employees_Traveling_with_You__c;
        TRInfo1.Trip_Reason__c=TRInfo.Trip_Reason__c;
        TRInfo1.Comments__c=TRInfo.Comments__c;
        TRInfo1.Reason_for_Late_Booking__c=TRInfo.Reason_for_Late_Booking__c;
        TRInfo1.Total_Air_Fare__c=TRInfo.Total_Air_Fare__c;
        TRInfo1.Air_Fare_Currency__c=TRInfo.Air_Fare_Currency__c;
    
        return TRInfo1;
   }
}

 

 

 

 

 

ministe2003ministe2003

In order to display errors on a Visualforce page, you need to have the <apex:pagemessages /> component in your page.

Just stick it above your form tag at the top.

ManidipaManidipa

the customer wants the error messages to be shown like the inbuilt validation error messages.But if i am adding <apex:pagemessages>i am not getting it in the same way,the meaage format is different .

ministe2003ministe2003

Well you can format the message to display slightly differently, check out the documentation.

bob_buzzardbob_buzzard

My understanding is that when you save a record, if there are validation errors that are specified to show at the field level, an error is added to the field.  Then when you are using an input field, Visualforce detects the added error and displays it against the field.

 

The problem that you have is that when you are cloning the record, you create a new instance and attempt to save that, so the errors are added to fields that aren't present on the page. Unfortunately, there doesn't appear to be any way to retrieve the errors that are associated with a field, so you can't just inspect the record that you attempted to add.

ManidipaManidipa

So is there any possible way to get the out of box validation error messages in the cloned page of a record?if i am adding <apex:pagemessages/>i am able to get the custom validation error message which is displaying at the top of the VF page,but i want it to be shown as in the out of box validation error message format.

bob_buzzardbob_buzzard

I think you'll need to change the way that a clone happens.  You need the inputfields on the page to be part of the record that you are attempting to save, so I'd suggest you want a page action attribute that does the cloning and replaces the record used in the page.

ManidipaManidipa

Thanks for the solution,but if Iam going for customization of clone button I have to customize so many other pages and functionalities  which are related to this. So, could you please provide any  alternate solution.

bob_buzzardbob_buzzard

I'm not talking about changing the clone button, but changing the way that your Visualforce page processes cloning.

 

How does this page get called for cloning at the moment?