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
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student 

Validation Errors appearing on visualforce Page please help

Hey there,

I have validation rules on my custom object service__c. Service__c, has a visualforce page for creating new services. Whenever a validation rule is triggered however, rather than the error appearing on te page in error, with no re-direction...it is re-directed to another page, which is all white, with black writing that says:

Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION

How can I make the errors appear on the visualforce page without re-direction? If it helps, I have made all the buttons custom.

I have already added <apex:pagemessages/> to the top of the page.

please Help
Best Answer chosen by Developer.mikie.Apex.Student
Swati GSwati G

Redirect to other page only if it saves successfully


try {             
   
      insert ser;
        // Send the user to the detail page for the new account.
     PageReference pageRef= new PageReference('/apex/DestinyAccount?id='+ser.account__c+'&Sfdc.override=1');
        pageRef.getParameters().put('tab','Destiny Services');
        return pageRef;

   
       } catch (Exception e) {     
      
             ApexPages.addMessages(e);         
        }
        return null;

All Answers

Swati GSwati G
Hi,

You will have to use try catch block.

Example: 

try {              
    
    //    Your code here
       } catch (Exception e) {      
       
             ApexPages.addMessages(e);          
        }
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Thank you for the reply Swati,

When you say my code, do you mean my entire Extension? or the button section? or just the insert line?
ppoojary18@gmail.comppoojary18@gmail.com
just a insert line as currently you only want to show validation message in page .
Swati GSwati G
You can use try catch with the insert line.
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
I tried it out like so, the line is highlighted.:

public class extSaveSerButton {
 
    public Service__c ser {get;set;}
   
    
    public extSaveSerButton(ApexPages.StandardController controller) {
   
        this.ser = (Service__c)controller.getRecord();
        
      

    }
    
    
    Public PageReference saveDestinyService(){
    


    if(System.currentPageReference().getParameters().get('clone') == '1'){

        //Set record id to null

        ser.Id = null; 

    }
         
       try {              
    
      insert ser;
        // Send the user to the detail page for the new account.
      
    
       } catch (Exception e) {      
       
             ApexPages.addMessages(e);          
        }
         PageReference pageRef= new PageReference('/apex/DestinyAccount?id='+ser.account__c+'&Sfdc.override=1');
        pageRef.getParameters().put('tab','Destiny Services');
        return pageRef;
  
    
    
  
    }
    
    Public PageReference saveNewDestinyService(){
    
    
        insert ser;
  
        // Send the user to the detail page for the new account.
       PageReference pageRef= new PageReference('/a0I/e');
        
        return pageRef;
    }
    
    
    
    
    Public PageReference cancelDestinyService(){
    PageReference pageRef = new PageReference('/apex/DestinyAccount?id='+ser.account__c+'&Sfdc.override=1');
    pageRef.getParameters().put('tab','Destiny Services');
    return pageRef;
    
    }
    
   

}




All it did was skip validation. Maybe I wrote it in wrong
Swati GSwati G

Redirect to other page only if it saves successfully


try {             
   
      insert ser;
        // Send the user to the detail page for the new account.
     PageReference pageRef= new PageReference('/apex/DestinyAccount?id='+ser.account__c+'&Sfdc.override=1');
        pageRef.getParameters().put('tab','Destiny Services');
        return pageRef;

   
       } catch (Exception e) {     
      
             ApexPages.addMessages(e);         
        }
        return null;
This was selected as the best answer
Developer.mikie.Apex.StudentDeveloper.mikie.Apex.Student
Thank you Swati, it works well.