1

I would like to display a related list from a custom object on my Case record. Please reference the information below:

  • Case Object: Contains a lookup field (Listing__c)
  • Listing Object: Master in a master-detail relationship with an object called Booking Rule(BookingRule__c)
  • Booking Rule Record: Due to the master-detial relationship, every Booking Rule record is associated to a Listing

Big Idea: The Case object and Booking Rule object are unrelated. However, they are somewhat related due to the Listing lookup field on the Case record. I would like to display the Booking Rules related list from my custom object (Listing__c) page layout on my Case page layout.

I hear a visualforce page will do the job. However, I'm unsure where to start, what APEX code to use and how to bring it all together.

Thanks in advance for anyone willing to help me out!

  • So you want to show all Booking Rules related to Listings related to Cases, but on the Case object? – Dan Jones Oct 6 '16 at 14:30
  • The Listing lookup field lives on the Case Record. I want to show all Booking Rules related to that Listing record as a section on the Case page layout. Basically, there will be a section, on the Case page layout, of all Booking Rules related to the Listing within the Case. – Larry Caper Oct 6 '16 at 14:34
1

Use the <apex:relatedList> tag. It's dead simple.

<apex:page standardController="Case">
    <apex:relatedList subject="{!Cae.Listing__c}" list="BookingRules__r" />
</apex:page>

You can run this script to get the verbatim list name:

for (ChildRelationship relation : SObjectType.Listing__c.getChildRelationships())
    if (relation.getChildSObject() == BookingRule__c.sObjectType)
        system.debug(relation.getRelationshipName());
  • I created the VF page, added the VF page to the layout and I received the following error: "Content cannot be displayed: 'BookingRules__r' is not a valid child relationship name for entity Listing". I don't understand, because when I look at the Schema Builder the Listing (Listing__c) object has a master-detail relationship with Booking Rule. @AdrianLarson – Larry Caper Oct 6 '16 at 17:36
  • Hence why I included the second script. Did you run it? Just paste into Execute Anonymous. – Adrian Larson Oct 6 '16 at 17:45
  • Oh ok, I'm an Admin, my apologies. However, when running the script will it affect any current code, code blocks, API connections or anything else in the system? – Larry Caper Oct 6 '16 at 17:53
  • 1
    Nope. It won't affect anything as you are running that in "Execute Anonymous Window" – SFDC_BigDog Oct 6 '16 at 17:59
  • 1
    No, Execute Anonymous will not affect your code base. It's throwaway code you can use for one off processing. – Adrian Larson Oct 6 '16 at 18:00
0

There are so many ways to do. Here is my approach to display booking related list in case page layout. 1. Create a lookup relationship in booking to case object. 2. create a trigger on booking object. Creation of every booking tag case record from using listing object. 3. Then add booking related list in case page layout.

2nd approach VF page. Vf page

<apex:page standardController="Case" extensions="BookingExtenion">
    <apex:pageblock>
    <apex:pageblockTable value="{!bookingList}" var="book">
        <apex:column value="{!book.name}" />
    </apex:pageblockTable>  
    </apex:pageblock>  
</apex:page>

and controller

public with sharing class BookingExtenion {
    public List<booking> bookingList {get;set;}
    public Case caseRecord {get;set;}
    public BookingExtenion(ApexPages.StandardController controller) {
        caseRecord  = (Case)controller.getRecord();
        Map<ID, Listing> listingMap = new Map<ID, booking>([SELECT Id, LastName FROM Listing where case__c=caseRecord.id]);
        bookingList = [select id,name from booking where listing__c in :listingMap.keySet()]
    }
}

then add this vf page in case page layout

  • Hey Shravan, I considered that too! but I do not want to add another lookup field to the Booking object. So, I believe the VF methodology is the next best step. I simply need to know how to write the controller and VF page. Thanks. – Larry Caper Oct 6 '16 at 15:28
  • @LarryCaper.modified answer with vf page approach – Shravan Boddula Oct 6 '16 at 16:14
  • Almost! thanks for the attempt, but I think there may be an error related to how the fields and/or objects are written. The Booking Rule (BookingRule__c) object is a child to the master Listing custom object (Listing__c). The Case record has a Listing Lookup field (Listing__c). – Larry Caper Oct 6 '16 at 16:35
  • @LarryCaper Shravan's solution is the easiest and best approach. That should be a start for you. If that solve your problem, please explain the issue more. – SFDC_BigDog Oct 6 '16 at 16:50
  • @SFDC_BigDog I completely agree, but I can't add another lookup field due to a potential coding issue. I was specifically asked to do this via VF and Controller. – Larry Caper Oct 6 '16 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.