Tuesday, 29 March 2016

Get relationship of an object using dynamic apex or metadata API

Dynamic List of Relations:

In below post i will share some very interesting business scenarios related to getting relations of an object dynamically in Salesforce.

When we normally talk about relations in salesforce. It will be considered in two ways with respect to an object.

  1. The object you are referencing is parent of how many objects in organization
  2. The object you are referencing is child of how many objects in your organization.
E.g: If you want to get relations of "Account" object in your org.Then what actually your requirement is? Get list of all objects which are child of "Account" object or get list of all objects which are parent(Lookup/reference on account object) of "Account".

Get Child Relationship:


In order to access child relationship of an sobject we can use getChildRelationships() method of salesforce.
This is  a built in method of "DescribeSObjectResult" class of Salesforce. You can also access record types, labels and CRUD rights of any sobject using this class methods.

Link to check more!!

Below method will take your object API name as parameter and will provide you list of selectoption of all child relationship in order to show on your VF page.

Apex Method:

public static List<SelectOption> GetObjectChildRelations(String ParentObject){
List<Selectoption> ListOfChildObjects = new List<Selectoption>();
SObjectType objObjectType = Schema.getGlobalDescribe().get('Account');
    //it gives object properties or describe results
    Schema.DescribeSObjectResult   describeresult = objObjectType.getDescribe();
    List<Schema.ChildRelationship>   lstchildrelationships =describeresult.getChildRelationships();
for(Schema.ChildRelationship relname:lstchildrelationships){
        ListOfRelatedObjects.add(new SelectOption(relname.getChildSObject(),relname.getRelationshipName()));
        System.debug('Relationshipname:'+relname.getChildSObject());                      
}
        return ListOfChildObjects ;
}

getGlobalDescrible:

Returns map of all sobject names(keys) to sobject token(values) for standard and custom object define in your organization.

Get Reference Relationship:

In order to access parent/reference relationship of an sobject we can use Reference() method of salesforce.
This is  a built in method of "DisplayType" class of Salesforce. You can check different field types using this class.

Link to check more!!

Below method will take your object API name as parameter and will provide you list of selectoption of all child relationship in order to show on your VF page.

Apex Method:

public static List<SelectOption> GetObjectRelatedRelations(String ParentObject){
    
List<Selectoption> ListOfRelatedObjects = new List<Selectoption>();
    Map<String, Schema.SObjectField> fieldsMap = new Map<String, Schema.SObjectField>();
          
    SObjectType objObjectType = Schema.getGlobalDescribe().get(ParentObject);
            
    //it gives object properties or describe results
    Schema.DescribeSObjectResult   describeresult = objObjectType.getDescribe();
    
    //Adding fields to the fieldsMap
    fieldsMap =  describeresult.fields.getMap();
// This for loop add the field api name as a key and field label as value into field list
    for(Schema.SObjectField fieldName:fieldsMap.Values()){
        
        //Add this if type is Reference then get object name else field API
        if(fieldName.getDescribe().getType() == Schema.DisplayType.Reference){
            ListOfRelatedObjects.add(new SelectOption(fieldname.getDescribe().getName(),fieldName.getDescribe().getLabel()));
            
        }
    }//end of for
    return  ListOfRelatedObjects;
}

VF Page Controller:

//Property to use on VF page
public List<selectoption> ObjectsShown {get;set;}

//Method to populate objects in select option list
public List<SelectOption> getObjectsRelation(){
ObjectsShown = new List<selectoption>();
ObjectsShown = GetObjectRelatedRelations(‘Opportunity’);
    return ObjectsShown;
}

Hope it helps to get relation objects dynamically.

No comments:

Post a Comment