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
MyGodItsColdMyGodItsCold 

Can related list objects be filtered with criteria

When I see an Account detail screen, I'd like to be able to only show Contacts based on some kind of criteria.

Can this be done? Is there an override, or controller extension that could do it? Or, could it be done via security rights?

Thanks,
ryan_mmryan_mm
Sure, you could do something like:

SELECT Name, (SELECT Name FROM Contact WHERE Country = 'USA') FROM Account
werewolfwerewolf
No, he's asking about a related list on a detail page.  You can only do it by overriding the detail page with a visualforce page and adding a VF related list to it; there's not yet any native way to filter related lists.
werewolfwerewolf
If it were an object other than Contacts than you might be able to do it with a sharing model, but since Contacts is controlled by parent, if you can see the account you can see the contacts.  So that won't work here.
MyGodItsColdMyGodItsCold
OK, I tried it. Maybe my approach is all wrong, but I don't know where to put my SELECT statement. Here's my VF page:


Code:
<apex:page>
  <apex:detail id="AcctOverRide_v1" relatedList="false">
      <apex:relatedList id='activeContacts' list='Contacts' title='Contacts'>
          <apex:facet name='body'>{! [SELECT Id Name Phone FROM Contacts WHERE Active__c = 1] }
          </apex:facet>
      </apex:relatedList>
  </apex:detail>
</apex:page>

 It doesn't Save - the message is:

Error: Syntax Error. Missing '='


MyGodItsColdMyGodItsCold
OK. (Yes, I did catch the Contacts vs Contact in the SOQL above)  I must put SOQL into Controller. How about this:

Here's the Page:

Code:
<apex:page standardController="Account" extensions="MyAcctExt">
  <apex:detail id="AcctOverRide_v1" relatedList="false">
      <apex:relatedList id="activeContacts" list="Contacts" title="Contacts" subject="{!ActContacts}">
          </apex:relatedList>
  </apex:detail>
</apex:page>

In MyAcctExt:

Code:
public class MyAcctExt {
    public List<Contact> ActiveContacts = new Contact[]{};
 
    public List<Contact> getActContacts() {
        ActiveContacts = [SELECT Id, Name, Phone FROM Contact WHERE Name contains 'Daniel'];
        return ActiveContacts;
    }
        
}

 But this won't Save -
Error: Compile Error: unexpected token: name at line 5 column 69
 
However, I've got custom controller where I use a similar SOQL statement


werewolfwerewolf
It's because contains is not a SOQL keyword.  Try instead:

Code:
public class MyAcctExt { 
    public List<Contact> getActContacts() {
        List<Contact> activeContacts = [SELECT Id, Name, Phone FROM Contact WHERE Name like '%Daniel%'];
        return activeContacts;
    }      
}

 

MyGodItsColdMyGodItsCold
Excellent. Thanks.

I'm thinking I'm very close, but I have 1 fear described in the following 2 points:

1. I'm not sure the apex:relatedList tag will get a top of the page entry of the related list -
    only as a section on the detail page proper - well, not exactly proper - just past the
    very end - under the '^ Back to Top' and 'Always show me  more related list records
    per page' links at the bottom of the page when the apex:detail tag's relatedList
    attribute is true.

2. Even setting apex:detail relatedList="true", I would have settled for a descriptive
    name, like "Active Only Contacts" for my custom related list, but even that doesn't
    get me the top of the page "Active Only Contacts (3)" kind of hot link which would
    respond to hover pull down list.

Here is the code which saves, but when run, gives me:

Insufficient Privileges You do not have the level of access necessary to perform the operation you requested. Please contact the owner of the record or your administrator if access is necessary.

However, I'm the owner & I'm a system administrator.

 Page:
Code:
<apex:page standardController="Account" extensions="MyAcctExt">
  <apex:detail id="AcctOverRide_v1" relatedList="true">
      <apex:relatedList id="activeContacts" list="Contacts" title="Active Only Contacts" subject="{!ActContacts}">
          </apex:relatedList>
  </apex:detail>
</apex:page>

 
Controller Extensions:

Code:
public class MyAcctExt {

    public MyAcctExt(ApexPages.StandardController controller) {
        
    }

    public List<Contact> ActiveContacts = new Contact[]{};
    
    public List<Contact> getActContacts() {
       ActiveContacts = [SELECT Id, Name, Phone FROM Contact WHERE account.id = :System.currentPageReference().getParameters().get('id') 
AND ACTIVE__c = '1']; return ActiveContacts; } }

 
As I said, I think I'm really close. If I get rid of    subject="{!actContacts}"    I can see the related list after all other related lists, but not at the top.

If I next set relatedList="false", I get the related list section at the bottom of the page.

I've got a feeling this is as good as it gets, but I'm hoping I'm wrong.

Thanks for all your help, I truly appreciate it.



AlanPerkinsAlanPerkins
You can achieve what you want, but I think you will need to sacrifice the <Apex:detail> section and handcraft a <apex:PageBlodk> with pageBlockSections and pageBlockSectionItems. You then should have complete control over the placement of each of the related lists.
More work but more flexible.
MyGodItsColdMyGodItsCold
It can't be done. The filtered out contacts could be 'hidden' by changing owner to user w/ a role that's not inline with other users' rights to view. But this makes them 'hidden' for all uses - not just a related list.

Using dataTable in Visualforce, the same knowledge could be shown, but the ListName(count) at the top of the screen with hover-over list display would not be there.


ExecbizExecbiz
But then aren't the filtered related contacts shown as just text, not as links to their respective contact records?
gbalakrigbalakri

Did you ever get this one to work. I am trying to do some thing similar and is having issues. The error I am getting is insufficient privilages

Girish

MyGodItsColdMyGodItsCold
Insufficient privilages usually happens to me when I don't have security set up right - the easiest is on a VF page, when you see the list of VF pages, click Security & allow the designated profiles to have access.
sfdeveloper9sfdeveloper9

I am trying to do something similar to this, like filtering the activities relatedlist for contact. can somebody let me know how to create a custom related list from a VF page. do I need to add the VF page in the page layout. my VF page looks similar to this

 

<apex:page standardController="contact" extensions="TouchpointController">
 <apex:detail id="ChildOpps_v1" relatedList="true"> 
      <apex:relatedList id="HistoryTasks" list="Task" title="History Activities" subject="{!HistoryActivities}">     
      </apex:relatedList>         
  </apex:detail>
</apex:page>

 

 

Please let me know how can I add a custom relatedlist to contact detail page.

 

Thanks in advance 

YP.

DcoderDcoder

Hi,

 

I am trying to implement the same. What I have observed is, when we want to show the related list, we can directly use a code like below:

 

<apex:page standardController="<custom object>" >
<apex:relatedList list="OpenActivities" title="custom related list" >
           </apex:relatedList>
       
</apex:page>

 

But, when I try to customize the records being shown in related list by doing :

 

<apex:page standardController="dealer__c" extensions="test_customRL">
<apex:relatedList list="OpenActivities" title="custom related list" subject="{!custrl}" >
           </apex:relatedList>
       
</apex:page>

 

and My Extension class code:

 

public class test_customRL {

    public dealer__c de{get;set;}
    public sub_dealer__c sd;
    public List<task> rl = new List<task>();

    public dealer__c getde(){

    if(de == null) de = new dealer__c();

    return de;

    }


    public void setde(){

    this.de = de;

    }


    public test_customRL(ApexPages.StandardController controller) {

    de = (dealer__c) controller.getrecord();

    de = [select id,name from dealer__c where id = :de.id];

    }


    public List<task> getcustrl() {

       sd = [select id,name from sub_dealer__c where dealer__c = :de.id];

       rl = [select id,whatid,subject,type from task where whatid = :sd.id];

       return rl;}

    public void setcustrl(){

    this.rl= rl;

    }
}

 

I am getting the error :

 

Unable to Access Page
Invalid parameter value "[00T900000020zEIEAY]" for parameter "id".

The ID shown is the ID of task associated to sub_dealer__c object. One thing, I need to understand is how to pass the customized list of records to related list to be shown on VF page.

 

Any ideas plz!!

 

 

Thanks

dcoder