2014-04-30 32 views
2

如何使用VF或APEX獲取相關列表,當我們通過URL傳遞ID和對象時,Ex假設我發送Account對象和ID,我應該只獲取與Account對象相關的列表,但不能獲取Account details 。如果我使用APEx:詳細信息我將獲得帳戶和它的相關列表,但我只需要相關列表。僅在salesforce中使用的相關列表

@Ivan這裏的代碼

控制器

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()得到( '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> 

我想顯示的ID(不只是名稱)的所有相關的清單,但參數列表中只需要文字

回答

2

我會欺騙和隱藏頁面塊,我不想用CSS;)這不是一個聰明的Apex'y解決方案,但它需要照顧很多東西(用戶不應該看到的對象和列表,頁面佈局他的個人資料一致,選擇記錄類型等)

開始頁面是這樣的:

<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> 

檢查創建的HTML,你應該會看到類似這樣的:

enter image description here

我只想去隱藏div負責「細節」部分。我已經決定由元素的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> 

你可能想通過類的所有孩子的div是DIV也許類來做到這一點...這兩種方法都可以打破,如果SF更改類名/ id生成的方法。這不太可能,但請牢記這一點。

更安全的方法是確保我們知道,生成將使用$Component全局變量的標識,但是當我用{!$Component.detail}它不產生正確的結果......

+0

謝謝它的工作:-) – user3589895

+0

酷:)請考慮在幾天的時間接受答案。這樣,你可以給予其他人幾天更多的聰明才智,但最終這些問題很有可能會被「忽略」。 – eyescream

1

我認爲你正在尋找<apex:relatedList> VF標籤。看到它的文檔here

+0

感謝伊萬..suppose我發送任何通用對象(它可以是自定義或標準對象)和Id通過URL,它應該只顯示相關的列表 – user3589895

+0

您可以在控制器中使用DescribeSObjectResult.getChildRelationships()來生成可用關係列表和然後在頁面上使用'' – IvanR

+0

迭代它,我能夠獲得子關係,但是當試圖通過使用 它給出下面的錯誤錯誤:文字值是屬性列表中必需的,有什麼方法可以顯示這些相關列表 – user3589895

相關問題