Tag Archives: Visualforce Page

Check User Access Permission To Visualforce Page In Apex

Sample Code:

List<SetupEntityAccess> seaList = [SELECT Id, Parent.Name, Parent.Profile.Name, Parent.Profile.Id FROM SetupEntityAccess
                                   WHERE Parent.Profile.Id = :UserInfo.getProfileId() AND SetupEntityId IN
                                   (SELECT Id FROM ApexPage WHERE Name = :your_VF_page AND NamespacePrefix = :your_namespace)];

if(!seaList.isEmpty()){
    //Login user have access to given VF page
}else{
    //login user don't have access to given VF page
}

Query Rows & Collection size exceeds maximum limit

Visualforce Page:

<apex:page controller="yourCustomcontroller" readonly="true">  
</apex:page>  

Please note using readonly="true" in Visualforce page, can increase Query Rows & Collection size:

  • Query Rows limits increased from 50001 to 1 million rows.
  • Collection size limits increased from 1001 to 10000.

Get All Salesforce Users in Role Hierarchy Using Apex

Controller:

Public with sharing class Sample{
    Public List<string> CusrrentUserRole{get;set;}
    Public List<String> RolesInHierarchy{get;set;}
    Public Sample(){
        CusrrentUserRole = New List<string>();
        CusrrentUserRole.add([SELECT UserRoleId FROM User Where Id =:UserInfo.getUserId() LIMIT 1].UserRoleId);
    }
    Public Void getRollInHierarchy(){
        
        RolesInHierarchy = New List<Id>();
        RolesInHierarchy = getAllRoleInHierarchy(CusrrentUserRole);
    }
    
    Public List<string> getAllRoleInHierarchy (List<Id> UserRole) {     
        List<string> currentRoleIds = new List<string>();     
        for(UserRole userRoleName :[SELECT Id,name FROM UserRole Where ParentRoleId  = :UserRole AND ParentRoleID != null]){    
            currentRoleIds.add(userRoleName.id);    
        }    
        if(currentRoleIds.size() > 0) {     
            currentRoleIds.addAll(getAllRoleInHierarchy(currentRoleIds)); 
        }    
        List<String> RoleList = New List<String>();  
        for(UserRole r:[SELECT Name From UserRole Where Id In:currentRoleIds]){
            RoleList.add(r.name);      
        }
        return RoleList;
    }        
}

Visualforce Page:

<apex:page controller="Sample">
 <apex:form>
   <apex:pageBlock>
     <apex:commandButton value="Show Roles below my Hierarchy" action="{!getRollInHierarchy}"/> 
   </apex:pageBlock>
   <apex:pageBlock >
   <b> Role below in Hierarchy are : </b>{!RolesInHierarchy}
   </apex:pageBlock>
  </apex:form>
</apex:page>

Output:

Dynamically Add Rows in Apex PageBlockTable

Controller:

public class Sample{
    
    Account acc = new Account();
    public List<Account> accList { get; set; }
    
    public Sample()
    {
        accList = new List<Account>();
        accList.add(acc);
    }
    
    Public void addAccount()
    {
        Account ac = new Account();
        accList.add(ac);
    }
}

Visualforce Page:

<apex:page Controller="Sample">
    <apex:form>
        <apex:pageBlock id="pbAcc">
            <apex:pageblockSection >
                <apex:pageBlockTable value="{!accList}" var="acc">
                    <apex:column headerValue="Account Name">
                        <apex:inputField value="{!acc.Name}"/>
                    </apex:column>
                    <apex:column headerValue="Account Number">
                        <apex:inputField value="{!acc.AccountNumber}"/>
                    </apex:column>
                    <apex:column headerValue="Account Type">
                        <apex:inputField value="{!acc.Type}"/>
                    </apex:column>
                    <apex:column headerValue="Industry">
                        <apex:inputField value="{!acc.Industry}"/>
                    </apex:column>
                </apex:pageBlockTable>
                <br/><apex:commandLink value="Add Row" action="{!addAccount}" reRender="pbAcc"/>   
            </apex:pageblockSection>        
            <apex:pageblockSection columns="1" >
                <apex:pageblockSectionItem >
                    <apex:commandButton value="Save" />
                    <apex:commandButton value="Cancel" />
                </apex:pageblockSectionItem>         
            </apex:pageblockSection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

Output: