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
Sreeram ChakrapaniSreeram Chakrapani 

If Statement in Visual Force

Hi,

I have a requirement where when a button is clicked, the first thing I need to do is to check the person profile, if it is the correct profile then continue with the processing of displaying the search fields in a visual force page but if it is not the correct profile then redirect the user to another Visual Force Page. I am new to Visual Force and would greatly appreciate if anybody could write a sample code as a starter for the above requirements

Thanks
SC

logontokartiklogontokartik
Hi Sreeram, 

If you want to do it solely on Visualforce page, you might need to use a little bit of Javascript. 

Here is sample code. 


<apex:page >
    <script>
    	function checkProfile(){
           var profileName = '{!$Profile.Name}';
            if(profileName == 'Some profile'){
                window.location.href = '/home/home.jsp';
            }else{
              document.getElementById('profileDiv').style.display = 'block';
            }
            return false;
        }
    </script>
    <apex:form>
    	<apex:commandButton value="Click me to show details" onclick="checkProfile(); return false;"/>
    	<div id="profileDiv" style="display:none">
            <apex:outputText value="I am {!$User.FirstName}. This is my Profile {!$Profile.Name}"></apex:outputText>
        </div>
    </apex:form>
</apex:page>

If you want to use a controller, then you must write an apex class and in the controller method for the CommandButton you need to verify the User's Profile and based on it you can decide whether to redirect or show the div/output panel



<apex:page controller="TestProfileCon">
    
    <apex:form>
    	<apex:commandButton value="Click me to show details" action="{!checkProfile}"/>
    	<apex:outputPanel id="profileDiv" rendered="{!isProfileValid}">
            <apex:outputText value="I am {!$User.FirstName}. This is my Profile {!$Profile.Name}"></apex:outputText>
		</apex:outputPanel>
    </apex:form>
</apex:page>



public class TestProfileCon {
	
    public boolean isProfileValid{get;set;}
    
    public TestProfileCon(){
        isProfileValid = false;
    }
    	
    
    public Pagereference checkProfile(){
        
		Profile currentProfile = [Select Id, Name from Profile where Id = :UserInfo.getProfileId()];
        
        if(currentProfile.Name == 'System Administrator'){
            isProfileValid = true;
        }else{
            Pagereference redirectPage = new Pagereference('/home/home.jsp');
            redirectPage.setRedirect(true);
            return redirectPage;
        }
        
        return null;
        
    }
    
}

Hope this helps.



James LoghryJames Loghry

Another way to do this without any user intervention is to use the action attribute in Visualforce.  More info on the action attribute and the apex:page element can be found here: http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_page.htm

Using the same method from logonkartik's answer, your VF might look like the following:

<apex:page controller="TestProfileCon" action="{!checkProfile}">
    <apex:form>
          <apex:outputText value="I am {!$User.FirstName}. This is my Profile {!$Profile.Name}" />
        </apex:outputPanel>
    </apex:form>
</apex:page>

Sreeram ChakrapaniSreeram Chakrapani
This is exactly what I was looking for, you both are awesome, thanks for the help