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
Becka DBecka D 

Display an unrelated list?

Looking to display a list of records within an unrelated object. We are making an employee review object, and would like to display all the accounts that user owns on the review object. We will be able to grab the user's ID on that review object - they aren't the owner, but are in a lookup field. I'm invisioning a bit of Visualforce, but I'm not sure how to get there. Any help is appreciated!

Best Answer chosen by Admin (Salesforce Developers) 
WillNWillN

here's something simlar I did for displaying all the donations associated with a contact record - let me know if you need help adapting:

 

Controller

public with sharing class donationhistory {
list<Opportunity> opplist;
 private final Contact contact;
    public donationhistory(ApexPages.StandardController controller) {
   
        this.contact = (Contact)controller.getRecord();
    }


    public list<Opportunity> getMyDonations() {
        try{
        list<Opportunity> opplist = [select id, amount, expectedrevenue, name, closedate, stagename from Opportunity where cv__Contact__c =: contact.id];
         return opplist;
        }
        catch(system.exception e){
        return null;
        }
        
       
    }


   }

 Page:

 

<apex:page standardcontroller="Contact" extensions="donationhistory"  showheader="true">
  <apex:pageblock >
      <apex:pageblocktable value="{!MyDonations}" var="Donations">
               <apex:column >
      <apex:facet name="header"><b>Name</b></apex:facet>
   
        {!Donations.Name}
       </apex:column>
              <apex:column >
        <apex:facet name="header"><b>Amount</b></apex:facet>
        {!Donations.Amount}
        
       </apex:column>
              <apex:column >
        <apex:facet name="header"><b>Close Date</b></apex:facet>
        {!Donations.CloseDate}
       </apex:column> 
       
              <apex:column >
        <apex:facet name="header"><b>Stage</b></apex:facet>
        {!Donations.StageName}
       </apex:column> 
      </apex:pageblocktable>
  </apex:pageblock>
</apex:page>

 

 

All Answers

WillNWillN

here's something simlar I did for displaying all the donations associated with a contact record - let me know if you need help adapting:

 

Controller

public with sharing class donationhistory {
list<Opportunity> opplist;
 private final Contact contact;
    public donationhistory(ApexPages.StandardController controller) {
   
        this.contact = (Contact)controller.getRecord();
    }


    public list<Opportunity> getMyDonations() {
        try{
        list<Opportunity> opplist = [select id, amount, expectedrevenue, name, closedate, stagename from Opportunity where cv__Contact__c =: contact.id];
         return opplist;
        }
        catch(system.exception e){
        return null;
        }
        
       
    }


   }

 Page:

 

<apex:page standardcontroller="Contact" extensions="donationhistory"  showheader="true">
  <apex:pageblock >
      <apex:pageblocktable value="{!MyDonations}" var="Donations">
               <apex:column >
      <apex:facet name="header"><b>Name</b></apex:facet>
   
        {!Donations.Name}
       </apex:column>
              <apex:column >
        <apex:facet name="header"><b>Amount</b></apex:facet>
        {!Donations.Amount}
        
       </apex:column>
              <apex:column >
        <apex:facet name="header"><b>Close Date</b></apex:facet>
        {!Donations.CloseDate}
       </apex:column> 
       
              <apex:column >
        <apex:facet name="header"><b>Stage</b></apex:facet>
        {!Donations.StageName}
       </apex:column> 
      </apex:pageblocktable>
  </apex:pageblock>
</apex:page>

 

 

This was selected as the best answer
WillNWillN

btw:  we call the page from a button on the contact record.

SFDCMattSFDCMatt

I've done something similar, same approach as Will. But we displayed the VF page as an in-line component on the record so the user wouldn't have to click a button. If you push it to the very bottom of the record detail section it'll (sort of) blend in and (kind of) look like the first related list.

Becka DBecka D

OK, here is what I've got so far. Getting an error with Development_Plan__r.Employee_Name__r.Id

 

 

public with sharing class AcctList {
list<account> Acctlist;
 private final Development_Plan__c IDP;
    public AcctList(ApexPages.StandardController controller) {
   
        this.Acctlist = (account)controller.getRecord();
    }

    public list<account> getMyAccounts() {
        try{
        list<account> Acctlist = [select id, name from Account where OwnerId =: Development_Plan__r.Employee_Name__r.Id];
         return Acctlist;
        }
        catch(system.exception e){
        return null;
        }     
    }
   }

 

 


WillNWillN

Why are you instantiating as an extension to the Account, rather than the Development_Plan?  You're getting the ownerid from the Dev Plan, then gettting the list of the accounts based on that ID, right?  I think if you just get rid of that section, it should work.  Thus:  

public with sharing class AcctList {
list<account> Acctlist;
 private final Development_Plan__c IDP;
    public DevPlan(ApexPages.StandardController controller) {
   
        this.DevPlan = (Development_Plan__c)controller.getRecord();
    }

    public list<account> getMyAccounts() {
        try{
        list<account> Acctlist = [select id, name from Account where OwnerId =: Development_Plan__c.Employee_Name];
         return Acctlist;
        }
        catch(system.exception e){
        return null;
        }     
    }
   }

 

 

 

SFDCMattSFDCMatt

Try this, yo. The controller.getRecord() needs to grab the current record you're on, which is the Developer Plan, right?

 

public with sharing class AcctList {
    list<account> Acctlist;
    Development_Plan__c IDP;
    
    public AcctList(ApexPages.StandardController controller) {
   
        //This actually needs to get the record that you're currently on
        //this.Acctlist = (account)controller.getRecord();
        //Try this instead...
        IDP = (Development_Plan__c) controller.getRecord();
    }

    public list<account> getMyAccounts() {
        try{
        list<account> Acctlist = [select id, name from Account where OwnerId =: IDP.Development_Plan__r.Employee_Name__r.Id];
         return Acctlist;
        }
        catch(system.exception e){
        return null;
        }     
    }
   }

 

 

Becka DBecka D

Still getting the variable does not exist error: " Variable does not exist: Development_Plan__r.Employee_Name__r.Id"

 

Not sure whym because it very clearly does exist...

WillNWillN

Is Employee name a lookup?  If so, then you've already got the id - should just be Employee_Name__c?

The RavanThe Ravan

Oh well.... was about to post it here. But guess problem is solved.

 

Cheers,

Sid

http://force.siddheshkabe.co.in

May the Force be with you.

Becka DBecka D

Now I'm getting an "invalid conversion from runtime type" error when trying to build my VF page. Obviously, I have no clue what I'm doing...

 

<apex:page standardcontroller="account" extensions="AcctList"  showheader="true">
  <apex:pageblock >
      <apex:pageblocktable value="{!accounts}" var="account">
               <apex:column >
      <apex:facet name="header"><b>Name</b></apex:facet>
   
        {!Account.Name}
       </apex:column>
      </apex:pageblocktable>
  </apex:pageblock>
</apex:page>

 

WillNWillN

The value in your pageblocktable should be your method "{!MyAccounts}", I think

Becka DBecka D

Now I'm back to having an issue with the class. Saying the error is with this line...

 

this.IDP = (Development_Plan__c) controller.getRecord();

 

James LoghryJames Loghry

The issue with above is that you have a page with an Account standard controller, and you are trying to convert the Account to a Development_Plan__c object.  Try changing the standardController attribute to standardController="Development_Plan__c"

varun gupta 102varun gupta 102
Thank you for such an herculean solution. Totally agreed with the concept. It really worked for me.