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
affuaffu 

How to call fields to child obj from another child obj

Hi,

 

I have three objects Obj1,Obj2 and Obj3

Relation between these three objects is Obj2 and Obj3 are the childs for Obj1

Requirement - i have created a visual force page in Obj2 and i want to call list of Obj3 records where Obj2 related with Obj1.

 

 

Thanks in Advance.....

Yoganand GadekarYoganand Gadekar

If your requirement is to fetch all Obj3 records related one Record of Obj1 to which this Obj2 record is asociated  frm which this VF page is called,

Then once you call the page, store Obje1 id in variable and query all Obj3 records where Obj1 id is the one which you stored in variable.

 

Mark this as answer and hit kudos if this post helps you.

affuaffu

Hi, 

Thanks for the reply.

Can you please send a sample code?

Yoganand GadekarYoganand Gadekar

Lets take up standard objects for example :

Opportunity(Obj 3) and contacts(Obj 2) are childs of account(Obj 1).

 

Following page called from contact, fecthes all opportunities of the account asociated with the contact from where this VF page is called.

System debug will show you all the opportunities of the account.

 

-- VF page--

 

<apex:page standardController="Contact" extensions="FetchRecordsController">
 <apex:form >
      <apex:pageblock ></apex:pageblock>
 </apex:form>
</apex:page>

 

-- Controller --

 

public with sharing class FetchRecordsController {
Contact ContactRec;
Id AccountID;
List<Opportunity> OpportunityList = New List<Opportunity>();
    public FetchRecordsController(ApexPages.StandardController controller) {
      ContactRec = (Contact)controller.getrecord();
      AccountId = ContactRec.AccountId;
      OpportunityList = [Select id,name from Opportunity where AccountId =:AccountId ];
      System.debug('**All Opportunity Records** of'+ AccountId + 'are' +OpportunityList );
    }

}

 

Hit kudos by clicking the blue star and mark this as answer if it helps you.

 

thanks and regards,

Yoganand.

affuaffu
Hi Yoganand,

Thanks for the example, I have written a class for the standard objects it
was working fine.
But i got stuck doing it for custom objects.
Can you please help how to do on custom objects on the same scenario.

Thanks.......
affuaffu

How to do it for custom objects

 

public class test {
public List<Contact> cont{get; set;}
public Id oppid;
public Id AccountId;
public test(ApexPages.StandardController controller) {
oppId= controller.getRecord().Id;
system.debug('*********************OppId'+oppId);
try
{
Opportunity opp=[Select id,AccountId From Opportunity Where id=:oppId];
system.debug('Opportunity%%%%%%%%%%'+opp.AccountId);
cont=[Select LastName,FirstName From Contact Where AccountId=:opp.Accountid];
}
catch(Exception e)
{
system.debug('Exception'+e);
}
}
}

 

 

Lets say Account = Obj1
Opportunity = Obj2
Contact = Obj3