[SalesForce] Display all the users and profiles assigned to each record

I have the following Class and the Page displaying all records from an object. I want to display all Users and Profiles assigned to each record in front of each record in separate columns. How do I modify my class and page to display assigned Users and Profiles ? Any help on this is appreciated!

    public with sharing class BusinessUnitMember{

    public List<grc__Business_Unit__c> theBusinessUnit {get; set;}
    public List<User> selectionUsers {get; set;}

    public BusinessUnitMember(ApexPages.StandardController con)
        {

           theBusinessUnit = [select Id, Name, OwnerId, Sharing_Group_Name__c,     Read_Write_Access_to_Child_BUs__c,
                               from grc__Business_Unit__c 
                               order by Name
                               limit 50000
                               ];

selectionUsers = [select UserRole.Name, Profile.Name, LastName, 
                                 IsActive, FirstName, UserType, Selected__c
                          from User
                          //where Id = : theBusinessUnit.OwnerId
                          //and IsActive = true
                          order by LastName];
    }
    }

VF Page

<apex:page standardController="grc__Business_Unit__c" extensions="BusinessUnitMember" standardStylesheets="true">
<apex:form id="form">
<apex:pageBlock title="Business Unit Member">
<apex:pageBlockTable value="{!theBusinessUnit}" var="dbItem">
<apex:column headerValue="Business Unit Name" ><br/>
<apex:outputLink value="/{!URLFOR(dbItem['Id'])}">{!dbItem.Name}</apex:outputLink>
</apex:column>
</apex:pageBlockTable>
</apex:pageBlock>
</apex:form> 
</apex:page>

Best Answer

Assuming its the owner you want to display, SOQL relationship queries allow several levels of parent object to be queried at once. So you can add the objects and fields your require to the query by using the parent object relationship names:

theBusinessUnit = [
        select Id, Name, Sharing_Group_Name__c, Read_Write_Access_to_Child_BUs__c,
                Owner.Name, Owner.Profile.Name
        from grc__Business_Unit__c 
        order by Name
        limit 50000
        ];

and then display the values by adding more columns in the Visualforce:

<apex:column headerValue="User" value="{!dbItem.Owner.Name}"/>
<apex:column headerValue="Profile" value="{!dbItem.Owner.Profile.Name}"/>
Related Topic