1

I am writing a hierarchy component in lightning which will display a tree structure for objects which have self lookup. For eg. Account, Case has a self lookup relationship. So component will display all grandparents and all children of a single Account(if exist). Component is valid only if object has a self lookup. I want to check whether object has any self lookup or not. Else my component will crash throwing exception. Any help will be appreciated.

1 Answer 1

6

you can achieve it with server-side controller.

public Boolean hasSelfReference(SObjectType sobjToCheck){
    List<SObjectField> objectFieldList = sobjToCheck.getDescribe().fields.getMap().values();
    Boolean result = false;
    for(SObjectField field :objectFieldList){
        DescribeFieldResult dresult = field.getDescribe();
        List<SObjectType> referencesTo = dresult.getReferenceTo();
        Set<SObjectType> referencesToSet = new Set<SObjectTYpe>(referencesTo);
        if(referencesToSet.contains(sobjToCheck)){
            result = true;
            break;
        }
    }
    return result;
}

For example,

System.debug(hasSelfReference(Account.SObjectType));

|DEBUG|true

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .