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
Lena ChristyLena Christy 

Display related records in a vf page of the user in the partner community

Hi everyone! I don't know if I'm asking too much, but I really need your help about this please!

I want to create a vf page that I put in the Community, where I want each user who's accesing to it, to see its own detail contact and related opportunities to its Account.
P.S. I enabled the "Partner User" for the contact and give him "Partner Community User" as a Profile.
Here is my code, I really hope you could help me with this.
<apex:page standardController="Contact" extensions="DetailPageCon">
<apex:form >
<apex:pageblock id="CustomList" title="Related Opportunities"  >
   <apex:pageBlockTable value="{!oppz}" var="o" rendered="{!NOT(ISNULL(oppz))}">
        <apex:column value="{!o.Name}"/>
        <apex:column value="{!o.Account.Name}"/>
        <apex:column value="{!o.Type}"/>
       <apex:column value="{!o.Amount}"></apex:column>
       <apex:column value="{!o.CloseDate}"/>
   </apex:pageBlockTable>
   <apex:outputLabel value="No records to display" rendered="{!(ISNULL(oppz))}" styleClass="noRowsHeader"></apex:outputLabel>
 </apex:pageblock>
</apex:form>
</apex:page>
The controller:
public class DetailPageCon {
    private List<Opportunity> oppz;
    private Contact cntact; 
    public DetailPageCon(ApexPages.StandardController controller) {
        this.cntact= (Contact)controller.getRecord();
    }
    public List<Opportunity> getOppz()
    {
        Contact con = [Select id, Account.id FROM Contact where id = :cntact.id];
        if (con.Account == null)
         return null;
        oppz = [Select id, Name, Account.Name, CloseDate, Amount, Type from Opportunity where Account.id = :con.Account.id];
        return oppz;
    }
}

 
Best Answer chosen by Lena Christy
James LoghryJames Loghry
In that case, you're going to want to change your controller to read the Contact info from the currently logged in user in your controller like below.  Note, you have to change the constructor to be the default constructor here, so that you can add the Visualfore page as a VF tab in your community.
public class DetailPageCon {
    public List<Opportunity> oppz {get; private set; }
    private Contact cntact; 
    public DetailPageCon() {
        User u = [Select ContactId From User Where Id = :UserInfo.getUserId()];
        this.cntact = [Select AccountId From Contact Where Id = :u.ContactId];
        this.oppz = [Select id, Name, Account.Name, CloseDate, Amount, Type from Opportunity where AccountId = :this.cntact.AccountId];
    }
}

Next, you'll remove the standardController / extension bit in your Visualforce page and replace it with the custom controller:
 
<apex:page controller="DetailPageCon">
<apex:form >
<apex:pageblock id="CustomList" title="Related Opportunities"  >
   <apex:pageBlockTable value="{!oppz}" var="o" rendered="{!NOT(ISNULL(oppz))}">
        <apex:column value="{!o.Name}"/>
        <apex:column value="{!o.Account.Name}"/>
        <apex:column value="{!o.Type}"/>
       <apex:column value="{!o.Amount}"></apex:column>
       <apex:column value="{!o.CloseDate}"/>
   </apex:pageBlockTable>
   <apex:outputLabel value="No records to display" rendered="{!(ISNULL(oppz))}" styleClass="noRowsHeader"></apex:outputLabel>
 </apex:pageblock>
</apex:form>
</apex:page>

Create the VF tab with the proper permissions for your community profile, and add it to the list of tabs in your community.

All Answers

James LoghryJames Loghry
Looks like it should work well enough.  I would suggest moving the query to your constructor though, because every time you reference "oppz" in your visualfore page, it's running two queries in the "getOppz" method.  To do this, implement your controller like so:
 
public class DetailPageCon {
    public List<Opportunity> oppz {get; private set; }
    private Contact cntact; 
    public DetailPageCon(ApexPages.StandardController controller) {
        this.cntact= (Contact)controller.getRecord();
        this.oppz = [Select id, Name, Account.Name, CloseDate, Amount, Type from Opportunity where AccountId = :this.cntact.AccountId];
    }
}

Then just call your page with the id parameter like so:

/apex/pages/<your page name goes here>?id=<id of the contact record goes here>



 
James LoghryJames Loghry
Also keep in mind that VF pages will work for the "former" version of communities, but if you're trying to display this on a Community Builder template like the Napili template, then you'll need to do this via Lightning Components instead.
Lena ChristyLena Christy

Thank you James Loghryy for your remark about the controller!

About the community, what I'm trying to do is when the partner user access to the community(SALESFORCE TABS + VISUALFORCE), he can see a Tab (where I want to make this VF page) so he can see his information about the contact and the opportunities of its Account.

I think that using Global Variables like $User, is the answer, but I really don't know to work with and use it in the controller ... If you could help me I would be grateful . 

James LoghryJames Loghry
In that case, you're going to want to change your controller to read the Contact info from the currently logged in user in your controller like below.  Note, you have to change the constructor to be the default constructor here, so that you can add the Visualfore page as a VF tab in your community.
public class DetailPageCon {
    public List<Opportunity> oppz {get; private set; }
    private Contact cntact; 
    public DetailPageCon() {
        User u = [Select ContactId From User Where Id = :UserInfo.getUserId()];
        this.cntact = [Select AccountId From Contact Where Id = :u.ContactId];
        this.oppz = [Select id, Name, Account.Name, CloseDate, Amount, Type from Opportunity where AccountId = :this.cntact.AccountId];
    }
}

Next, you'll remove the standardController / extension bit in your Visualforce page and replace it with the custom controller:
 
<apex:page controller="DetailPageCon">
<apex:form >
<apex:pageblock id="CustomList" title="Related Opportunities"  >
   <apex:pageBlockTable value="{!oppz}" var="o" rendered="{!NOT(ISNULL(oppz))}">
        <apex:column value="{!o.Name}"/>
        <apex:column value="{!o.Account.Name}"/>
        <apex:column value="{!o.Type}"/>
       <apex:column value="{!o.Amount}"></apex:column>
       <apex:column value="{!o.CloseDate}"/>
   </apex:pageBlockTable>
   <apex:outputLabel value="No records to display" rendered="{!(ISNULL(oppz))}" styleClass="noRowsHeader"></apex:outputLabel>
 </apex:pageblock>
</apex:form>
</apex:page>

Create the VF tab with the proper permissions for your community profile, and add it to the list of tabs in your community.
This was selected as the best answer