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
Sam1980Sam1980 

IF Else Condition in APEX- Depending on user Role

I have developed a VF Component and want to do the following so was wondering if its possible and any code will be great help

So if The logged in persons Role is  NOT a  Programmer i want to display the following BLOCK

<apex:component  controller="MyController">   
    <apex:pageBlock title="Products">
        <apex:pageBlockTable >
        </apex:pageBlockTable>
    </apex:pageBlock>

and If the role is programmer i want to display a message

<div> You are not the user </div>

1) How to check the role of current logged in used in APEX
2) How to use IF Else Condition in APEX
Best Answer chosen by Sam1980
Sam1980Sam1980
This solve my problem

<apex:pageBlock title="Products" rendered="{!$Profile.Name != 'Programmer'}">

All Answers

Boris BachovskiBoris Bachovski
1) You can use the global variable called $UserRole, together with the "rendered" attribute:
<apex:component controller="MyController">
	<apex:pageBlock title="Products" rendered="{!$UserRole.Name != 'Programmer'}">
	    <apex:pageBlockTable >
	    </apex:pageBlockTable>
	</apex:pageBlock>
	<apex:outputPanel rendered="{!$UserRole.Name == 'Programmer'}">
		<div> You are not the user </div>
	</apex:outputPanel>
</apex:component>
2) IF statement just like every formula field or validation rule:
<apex:outputText value="{!IF($UserRole.Name == 'Programmer', 'Programmer', 'Not a Programmer'}" />

Gopal RathoreGopal Rathore
Hi Sach,

This Is Controller
public class userRole {
public list<user> u {get; set;}
public string l = Userinfo.getUserId();
public boolean val {get; set;}
    public userRole(){
        u =[SELECT id FROM User WHERE UserRoleId = '00Ei00000014D7cEAE' AND ID=:l];
        if(u.size()>0){
        val = true;
        }
     }
}
Replace UserRoleId With Your Programmer Id.

This Is Vf Page
<apex:page controller="userRole">
  <apex:pageBlock rendered="{!NOT(val)}">      
      <p>This Is Not Programmer</p>
  </apex:pageBlock>

  <apex:pageBlock rendered="{!val}">
      <p>This Is Programmer </p>
  </apex:pageBlock>
</apex:page>

Regards
Gopal Rathore

Sam1980Sam1980
This solve my problem

<apex:pageBlock title="Products" rendered="{!$Profile.Name != 'Programmer'}">

This was selected as the best answer