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
vishnu sundarvishnu sundar 

Get Top Record in Self Relationship

I have custom object called RoleLevel__c . Supervisor__c is self lookup in RoleLevel__c. My requirement is to get the Top record in RoleLevel__c. for Example,

I am having records as below R1->R2->R3->R4->R5->R6->R7->R7->R8->R9->R10->R11

R11(record) have Supervisor__c lookup to R10(record), R10(record) have Supervisor__c lookup to R9(record) like wise it goes

we have another field names TopRole__C which would be updated by before trigger based on the Supervisor__c it gets the next record until Supervisor__c become null ... currently we are achieving this functionality by SOQL inside while loop to get toprecord . Any possibility to get the Top record without multiple SOQL ?
Iterating something like Supervisor__r.Supervisor__r.Supervisor__r.id did not work after 5 levels. R1 , R2 names are used for example .
SwethaSwetha (Salesforce Developers) 
HI Vishnu,

This is the same query as posted in https://salesforce.stackexchange.com/questions/304748/get-top-record-in-self-relationship

Have you been able to try the approach mentioned in the above post. If so, please mark this answer as best so that others facing the same issue will find this information useful.
 
Thank you