Activity Forums Salesforce® Discussions How can we find the object name based on the parent Id in the easiest way?

  • Extentia

    Member
    November 30, 2016 at 7:24 am

    Hi Mohit,

    One liner answer to this question can be by using below line we can get object name based on parent id :
    List<Schema.ChildRelationship> lstCR = <parentID>.getSObjectType().getDescribe().getChildRelationships() ;

    Below is the detailed example to get child object names (list of string) by parent ID :

    ID idToGet = '<Any Parent Object ID>';
    List<string> lstChildObj = GetChildObjects(idToGet);
    System.debug(lstChildObj);

    // Below function accepts 18 digit parent ID and returns list of Child Object 's API name.
    public List<String> GetChildObjects(ID parentID)
    {
    List<string> lstChildObjects = new List<string>();
    Schema.DescribeSObjectResult parentDescribeResult = r
    String sObjName = parentDescribeResult.getName();
    System.Debug('Parent object name : ' + sObjName);
    for (Schema.ChildRelationship childRel: parentDescribeResult.getChildRelationships())
    {
    lstChildObjects.add(childRel.getChildSObject().getDescribe().getName());
    }
    return lstChildObjects;
    }

    Thanks,

    Extentia

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos