Play Games

Search This Blog

Tuesday, November 17, 2020

get all the child objects of a parent object salesforce - Salesforce Globe For You

 get all the child objects of a parent object salesforce  - Salesforce Globe For You 

In this example,all the child objects of opportunity object are fetched.

Apex Class:OpportunityChildObjectController

public class OpportunityChildObjectController {

    public list<String> lstOppChildObjects {get;set;}

    public void getChildObjectsOfOpp() {

        lstOppChildObjects = new List<String>();

        Schema.DescribeSObjectResult objSchemaResult = Opportunity.SObjectType.getDescribe();

        for (Schema.ChildRelationship objChildRelationship : objSchemaResult.getChildRelationships()) {

            lstOppChildObjects.add(string.valueOf(objChildRelationship.getChildSObject()));

        }

        system.debug('Opportunity Related Child Objects are:'+lstOppChildObjects);

    }

}

Visualforce Page:OpportunityChildObjects

<apex:page controller="OpportunityChildObjectController" action="{!getChildObjectsOfOpp}">

    <b>Opportunity Child Objects are:</b><br/>

    <apex:variable var="index" value="{!1}" />

    <apex:repeat value="{!lstOppChildObjects}" var="objName" id="OppRelatedChildObjects">

        {!index}) <apex:outputText value="{!objName}" id="theValue"/><br/>

        <apex:variable var="index" value="{!index + 1}" />

    </apex:repeat>

</apex:page>

Output:



No comments:

Post a Comment