We are trying to remove all the custom buttons which was using Java Script code and replacing the same with VF buttons.(Migrating towards lightning)

However, i am not able to display the validation error message on my standard page layout(where the button is actually placed) which we are trying to validate on the apex controller class.

Example :

Controller Code

if(acc.phone == '' || acc.phone == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Account phone'));

VisualForce Code

<apex:pageMessages id="showmsg"></apex:pageMessages>

Is there anyway we can display the validation error message from my controller code to the standard page layout or even custom VF page in some cases.

Thanks in advance and appreciate your help.


  • By definition, detail page buttons that are VF will open a VF page where the errors, if any, will appear. You'll need to elaborate using edit more of your use case
    – cropredy
    Commented Jan 26, 2017 at 5:16

2 Answers 2


Normally, I use to follow this approach. Create a CustomException class.

public class CustomException extends Exception {}


    if(acc.phone == '' || acc.phone == null)
        throw new CustomException('Please enter Account phone');
catch(Exception e)
    ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error, ex.getMessage());

Now, error messages can be handled in the Exception block and displayed to the visualforce page inside <apex:pageMessages id="showmsg"></apex:pageMessages>


My VF code

<apex:page standardController="Contact" extensions="Restore_Former_User" action="{!formerUserUpdate}" >
<apex:pageMessages ></apex:pageMessages>
<apex:sectionHeader title="Open Salesforce Case and Pop Page"/>   
<apex:outputPanel >If you see this page, something went wrong. Please contact [email protected] for assistance.


Controller code

 public class Restore_Former_User {
Contact cont=new Contact();
Account acc=new Account();
Contact conobj=new Contact();
string strConId;
set<String> tmpString = new set<String>();
id accId;
boolean validateformeruser;
    public Restore_Former_User(ApexPages.StandardController stdcontroller)  {
            this.conobj= (Contact)stdController.getRecord();   
            System.debug('The value of the current conobj-------------------------->'+conobj);  
          // Code invoked on page load.      
    public PageReference formerUserUpdate()  {           
            String strConId = ApexPages.currentPage().getParameters().get('id');
            System.debug('The value of the current Contact ID-------------------------->'+strConId ); 
            cont=[select id,OMG_ContactType__c,OMG_FormerUserRel__c ,account.id,Clear_Trust_User_Name__c from contact where id=:strConId limit 1];
            System.debug('The value of cont is---->'+cont);

            if(cont.OMG_ContactType__c != Null){
            System.debug('The value of the tmpString-------------------------->'+tmpString); 

            String[] stringList = new String[0];
            if(tmpString.size() > 0){
                validateformeruser = checkformeruser (tmpString);
                System.debug('The value of the validateformeruser-------------------------->'+validateformeruser); 
                if(validateformeruser == False){
                System.debug('<--------------------------Not Former User--------------------->'); 
                //ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Type has to be Former User'));
                if(!tmpString.contains('Former User')){
                    throw new CustomException('Type has to be Former User');
                catch(Exception ex){
                ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.Error, ex.getMessage());
                //ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info, 'Type has to be Former User'));
                apexPages.Message msg = new Apexpages.Message(ApexPages.Severity.Info,'Type has to be Former User');
                    System.debug('Contains Former User -------------------------->'+tmpString);
                    tmpString.remove('Former User');
                    System.debug('The value of the Former User removal-------------------------->'+tmpString);
                    if(tmpString.size() == 0){
                        cont.OMG_ContactType__c = 'User'; 
                        System.debug('<------------------The value of tmpString is inside NUll condition-------------------------->');                      
                    for(string item: tmpString) {
                    System.debug('The value of the stringList-------------------------->'+stringList); 
                    String result = String.join(stringList,';');
                    System.debug('The value of the result-------------------------->'+result);  
                    cont.OMG_ContactType__c = result;
            update cont;
            catch(System.DMLException e){
            for(Integer i=0;i<e.getNumDml();i++)  {

            if(cont.Clear_Trust_User_Name__c != Null){
            String sessionID = UserInfo.getSessionId();  
            string url = 'https://'+Label.SalesforceUtilityServer+'/contact2.0/restoreFormerUser.html?id='+cont.Id+'session='+sessionID;
            //var popURL = "https://{! $Label.SalesforceUtilityServer }/contact2.0/restoreFormerUser.html?id={!Contact.Id}&session={!$Api.Session_ID}";            
            PageReference reference=new PageReference(url);
            return reference;                  
            // Send the user to the detail page for the case that gets updated.
            PageReference contactPage= new ApexPages.StandardController(cont).view();
            return contactPage;
            return null;
        public boolean checkformeruser (set<String> strg){
            boolean bol;
            if(!tmpString.contains('Former User')){
                bol = False ;           
                bol = True ;            
            return bol;

i am hitting this button from Contact detail page (Standard)

i getting the error message but in a vf page not on the standard contact detail page , screen shot below

enter image description here

i need the error to be on the standard contact detail page from where this button is hit .

Thanks for the help

  • @Santanu Boral thank you , this is code , can you please help Commented Jan 26, 2017 at 6:21
  • The error message will be displayed in the Visualforce page and not in standard page. You can embed the visualforce in Standard Pagelayout and could show the error. I think that will work for you Commented Jan 26, 2017 at 6:25
  • @SantanuBoral Can i try this approach , from the custom button Visual force page , i will write a Onload javascript function and validate some field values and throw a popup error message? Commented Jan 26, 2017 at 7:09
  • yes you can try Commented Jan 26, 2017 at 17:20
  • Can you close this question Commented Jan 26, 2017 at 17:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .