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
Bharat KumarBharat Kumar 

Self Relationship

Hi I am using one object "Location__c".

 

There are two fields Name and Location2__c(lookup to Location__c SELF RELATION).

 

Here I need to display the values in tree hierarchy like,

 

X

---x.1

---x.2

------x.1.1

------x.1.2

Y

---y.1

 

These are to be stored in the same object..

 

I am not getting to display these values in 3 level tree structure could you please help out on this

 

THank you 

Ashish_SFDCAshish_SFDC
Hi Bharath, 

See the link below to display level of Self Relationships, 

o display for example only Parent Accounts all you need is

SELECT Id, Name FROM Account WHERE ParentId = null
When your user decides he wants to view children of one record (which could be done as <apex:actionFunction> on the checkbox for example) you will be able to query up to 5 + 1 levels deep. Start with this query and experiment

SELECT Id, Name, (optional subquery here)
FROM Account
WHERE ParentId = :selectedId
    OR Parent.ParentId = :selectedId
    OR Parent.Parent.ParentId = :selectedId
    ... // We can use up to 5 dots
ORDER BY ParentId, Parent.ParentId, Parent.Parent.ParentId ...
By 5+1 I mean 5 "dots" and an option for subquery. It will not work on Accounts but on your custom object you should be able to put (SELECT Id FROM CustomObjects__r) there as well.

Then you'll have to somehow display them nicely in a tree ;) There are lots of Javascript tools that accept hierarchical JSON data or you might decide to build lots of nested <apex:repeat>... Just watch out for hitting viewstate limits!

http://stackoverflow.com/questions/15938292/making-a-tree-like-structure-in-visualforce-apex-from-a-self-referencing-table-s

Regards,
Ashish