Question

How to get only related lists using VF or APEX, when we pass ID and object through URL, Ex suppose I send Account Object and ID, I should be getting only Account object related lists but not Account details. if I use APEx:Detail I will get account and it’s related lists, but I want only related lists.

@Ivan here the code

controller

Public Class CustomRelatedList{


public String objectType {get; set;}

public Set<String>  chsobject1 {get;set;}
public sobjecttype chsobject {get;set;}

public sObject[] data {get; set;}


public CustomRelatedList() {
chsobject1 = new Set<String>(); 
 }


public void CustomRelatedList1() {
  //get URL parameters

// objectType = ApexPages.currentPage().getParameters().get('urlparm');

objectType = ‘Account’; //for testing purpose will get object type & Id from URL

Map<String , Schema.SObjectType> globalDescription =     Schema.getGlobalDescribe();

  Schema.sObjectType sObjType = globalDescription.get(objectType);  
    Schema.DescribeSObjectResult r1 = sObjType.getDescribe(); 

  List<Schema.ChildRelationship> C = r1.getChildRelationships(); 

for (Schema.ChildRelationship C1:C) {
       chsobject = C1.getChildSObject(); 
     chsobject1.add(String.valueof(chsobject));
}

  }
}

 VF page
<apex:page controller="CustomRelatedList" >
<apex:repeat value="{!chsobject1}" var="a" >
<apex:relatedList list="{!a}" subject="{!$CurrentPage.parameters.id}" /> 
</apex:repeat>
</apex:page>

I want to display the all related List for the Id(not just names) , but the parameter list takes only Literal

Was it helpful?

Solution

I would cheat and hide the page block I don't want with CSS ;) It's not a clever Apex'y solution but it takes care of sooo much stuff (objects and lists the user is not supposed to see, page layout consistent with his profile and selected record type etc.)

Start with page like this:

<apex:page id="page" showHeader="false" readonly="true">
    <apex:pageMessage severity="error" strength="2" rendered="{!ISBLANK($CurrentPage.parameters.id)}"
        summary="Id not passed" />
    <apex:detail id="detail" subject="{!$CurrentPage.parameters.id}" 
        title="false" showChatter="false" relatedListHover="false"/>
</apex:page>

Inspect the created HTML, you should see something similar to this:

enter image description here

I'd just go and hide the div's responsible for the "detail" part. I've decided to do it by the element's Id:

<apex:page id="page" showHeader="false" readonly="true">
    <style>
    div#ep_page_detail{
        display:none;
    }
    </style>

    <apex:pageMessage severity="error" strength="2" summary="Id not passed" rendered="{!ISBLANK($CurrentPage.parameters.id)}" />

    <apex:detail id="detail" subject="{!$CurrentPage.parameters.id}" 
        title="false" showChatter="false" relatedListHover="false"/>
</apex:page>

You might want to do it by class of that div or maybe classes of all child divs... Both options can break if SF changes the class names / method of the id generation. It's unlikely but just keep that in mind.

A more secure way to be sure the we know the Id that was generated would be to use the $Component global variable but it wasn't producing the right results when I've used {!$Component.detail}...

OTHER TIPS

I think you are looking for <apex:relatedList> VF tag. See it's documentation here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top