0

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.

Venkatesh

1
  • 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
    Jan 26, 2017 at 5:16

2 Answers 2

1

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

public class CustomException extends Exception {}

Controller

try
{
    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());
    ApexPages.addMessage(msg);
}

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

0

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.
</apex:outputPanel> 
</apex:page>

and

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){
            tmpString.addAll(cont.OMG_ContactType__c.split(';'));
            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'));
                try{
                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(msg);          
                }
                //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');
                ApexPages.addmessage(msg);              
                }
                else{
                    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-------------------------->');                      
                    }else{
                    for(string item: tmpString) {
                    stringList.add(String.valueOf(item));
                    }
                    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;
                    }
                accId=cont.account.id;
                cont.accountId=cont.OMG_FormerUserRel__c;
                cont.OMG_FormerUserRel__c=accId;
                 try{
            update cont;
            }
            catch(System.DMLException e){
            for(Integer i=0;i<e.getNumDml();i++)  {
            System.debug(e.getDMLMessage(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);
            reference.setRedirect(true);
            return reference;                  
            }else{        
            // Send the user to the detail page for the case that gets updated.
            PageReference contactPage= new ApexPages.StandardController(cont).view();
            contactPage.setRedirect(true);
            return contactPage;
            }           
            }          
            }    
            return null;
        }
        public boolean checkformeruser (set<String> strg){
            boolean bol;
            if(!tmpString.contains('Former User')){
                bol = False ;           
            }else{
                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

5
  • @Santanu Boral thank you , this is code , can you please help 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 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? Jan 26, 2017 at 7:09
  • yes you can try Jan 26, 2017 at 17:20
  • Can you close this question 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 .