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
Patrick ConnerPatrick Conner 

Two-Way Related List for Custom Object

I've created a Custom Object "Related List A", with two custom lookup fields: "Current" and "Related", with Related List Labels "Current" and "Related" respectively. The Related List is to be added to a custom account object "PPI".

If I'm in PPI Account A and add another PPI Account (B) to the Related field it will populate under the Current list label for Account A and under the Related List label for Account B. So to make these Accounts have a two-way relationship, I'd have to add both related lists "Current" and "Related" to the Related List A layout, which looks crappy. 

I'm wondering if there is a way to create a two-way relationship with one addition into a related field list? If I associate PPI Account B with PPI Account A in the Related field, why can't they both appear in each other's Related Lists?

This may be convoluted, it's difficult for me to lay it out without showing, haha. I hope someone can help. Thanks!!
Best Answer chosen by Patrick Conner
Alex TennantAlex Tennant

You have two options:

  1. Override your PPI Account's View page with a VIsualforce page and then either write a Controller Extension for it and put the Visualforce directly in the page, or write a Visualforce component and include it in the page. Your extension/component will query for these related objects and create a single view of them (replacing your two related lists)
  2. Write a trigger which manages the 'reverse relationships' for you. i.e. If you insert a relationship between two objects it inserts one with points the other way, likewise when you update and delete the relationships you need to perform the appropriate operation to the reverse one (I recommend a lookup to the 'reverse' relationship to easily locate them for these operations). YOu can then just display one related list on your page to get the full view.

All Answers

Alex TennantAlex Tennant

You have two options:

  1. Override your PPI Account's View page with a VIsualforce page and then either write a Controller Extension for it and put the Visualforce directly in the page, or write a Visualforce component and include it in the page. Your extension/component will query for these related objects and create a single view of them (replacing your two related lists)
  2. Write a trigger which manages the 'reverse relationships' for you. i.e. If you insert a relationship between two objects it inserts one with points the other way, likewise when you update and delete the relationships you need to perform the appropriate operation to the reverse one (I recommend a lookup to the 'reverse' relationship to easily locate them for these operations). YOu can then just display one related list on your page to get the full view.
This was selected as the best answer
Patrick ConnerPatrick Conner
This is exactly what I was looking for. Both involved, but relatively straightforward solutions. Thanks Alex!