+ Start a Discussion
SV MSV M 

Display error on VF without Custom Controller

Hi,

I want to display an error message for the user who doesn't have Read Access to Lead Object when he opens a VF Page without using a custom controller. Is it possible to achieve it from VF Page itself...
Thanks in Advance...
Best Answer chosen by SV M
AbhishekAbhishek (Salesforce Developers) 
We can implement this requirement by creating a new instance of ApexPages.message and then adding message to Apexpages using ApexPages.addmessage. Then displaying these messages in visualforce page.

We can add 5 different types of message in Visualforce Page. In the example below, we are showing 5 input fields of account. We have added a button on visualforce page. Different type of message will be shown on visualforce page if we will keep any field blank.


Please follow the below link for more info

http://www.sfdcpoint.com/salesforce/show-error-message-visualforce-page/

http://sfdcsrini.blogspot.com/2014/12/show-error-message-in-visualforce-page.html

Try the below code.

Apex:-
if (fieldname == null)
{
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter value'));
    return null;

 
Insert the below tag inside the visualforce page within page block
 
<apex:pageMessages ></apex:pageMessages>


You have to make some changes based on your scnerio.


Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.
 

All Answers

AbhishekAbhishek (Salesforce Developers) 
It's not possible.

If it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.
SV MSV M
Can I do it from Custom Controller. If yes, how can I achieve this...
AbhishekAbhishek (Salesforce Developers) 
We can implement this requirement by creating a new instance of ApexPages.message and then adding message to Apexpages using ApexPages.addmessage. Then displaying these messages in visualforce page.

We can add 5 different types of message in Visualforce Page. In the example below, we are showing 5 input fields of account. We have added a button on visualforce page. Different type of message will be shown on visualforce page if we will keep any field blank.


Please follow the below link for more info

http://www.sfdcpoint.com/salesforce/show-error-message-visualforce-page/

http://sfdcsrini.blogspot.com/2014/12/show-error-message-in-visualforce-page.html

Try the below code.

Apex:-
if (fieldname == null)
{
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter value'));
    return null;

 
Insert the below tag inside the visualforce page within page block
 
<apex:pageMessages ></apex:pageMessages>


You have to make some changes based on your scnerio.


Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.

Thanks.
 
This was selected as the best answer
SV MSV M
Thanks for the Information. I am trying to show error to a specific user by hard coding the User Id. Here is my Controller

//Controller
public class DisplayErroronVFPageController {
    public List<Contact> conList{get;set;}
    public DisplayErroronVFPageController() {
        List<User> usrList = [SELECT Id, Name FROM USER];
        for(User usr : usrList) {
            if(usr.Id == '0052w000008Q2lN') {
                ApexPages.addmessage(new ApexPages.message(ApexPages.severity.Error,'Preview Restricted'));
                //return null;
            }
            else {
                conList = [SELECT Id, FirstName, LastName FROM Contact LIMIT 10];
            }
        }
    }
}
//Page
 
<apex:page controller="DisplayErroronVFPageController">
    <apex:form>
        <apex:pageMessages/>
        <apex:pageBlock>
            <apex:pageBlockTable value="{!conList}" var="con">
                <apex:column headerValue="First Name" value="{!con.FirstName}"/>
                <apex:column headerValue="Last Name" value="{!con.LastName}"/>
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
I am getting error for every user. How to solve this....