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
samrat.1985@lntinfotechsamrat.1985@lntinfotech 

Control visibility of buttons based on User Logged in

I have a visual force page which is displaying results and form based on webservice. Which is working fine.

 

Now i have a scenario. There is a apex command button which should be visible only to User A and not to other users.

 How to make this scenario work in the visual force page??

Best Answer chosen by Admin (Salesforce Developers) 
WesNolte__cWesNolte__c

Sure, working from my phone at the moment so I can't test, it's either:

 

<apex:pageBlockButtons rendered="{!if($User.FirstName=='Samrat','true', 'false')}">

 

or

 

<apex:pageBlockButtons rendered="{!if($User.FirstName=='Samrat',true, false)}">

 

Also, the IF-statement is probably case-sensitive.

 

Wes

All Answers

WesNolte__cWesNolte__c

Hey

 

There are several ways to do this. One of the easier ways it to use something like {!IF($Profile.id=='TheProfileId',true,false)} in your visualforce page in a "rendered" component attribute. I'd advise putting the logic into your Apex controller though and querying the Profile object and return a boolean to determine what should be displayed to whom.

 

The Global Variable docs for VF can be found here: http://www.salesforce.com/us/developer/docs/pages/Content/pages_variables_global.htm

 

Wes

samrat.1985@lntinfotechsamrat.1985@lntinfotech

greteful if you can paste a sample code please as i tried

 

<apex:pageBlockButtons rendered="{!if($User.FirstName=='Samrat','visible:true', 'visible:false')}">

 

and it is not displaying the button section in both the cases :(

WesNolte__cWesNolte__c

Sure, working from my phone at the moment so I can't test, it's either:

 

<apex:pageBlockButtons rendered="{!if($User.FirstName=='Samrat','true', 'false')}">

 

or

 

<apex:pageBlockButtons rendered="{!if($User.FirstName=='Samrat',true, false)}">

 

Also, the IF-statement is probably case-sensitive.

 

Wes

This was selected as the best answer
samrat.1985@lntinfotechsamrat.1985@lntinfotech

hey thanks it works...

How can i get the check done on my controller class??

I just want to get the username who has logged in and control it from the controoller class.

samrat.1985@lntinfotechsamrat.1985@lntinfotech

Well thanks again....i was able to achive my functiopnality using the controller class.

 

here is the class

 public string firstName=UserInfo.getFirstName();
   
    public boolean visible=FALSE;
    public boolean getVisibile()
    {
        if(firstName!='Samrat')
        {
             visible=TRUE;
        }
        else
        {
             visible=FALSE;  
        }
        return visible;
    }

and following th the VF code

<apex:pageBlockButtons rendered="{!Visibile}">        <apex:commandButton id="myButton" value="Go!" action="{!HelloWorld}" />
        <apex:outputText value="{!$User.FirstName}"/>
        </apex:pageBlockButtons>
 

Slowly but sure getting a hang of Apex and VF now.. thanks for helping me out