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
DipakDipak 

How to fetch parent Table Data from a SObject

Hi,

 

I have designed two custom objects(say Table1 and Table2) having parent-child relationship. Table2 has one master-detail field(say field1).

I am trying to fetch the from two tables by following query

 

list<sobject> objlist= Database.query('SELECT Id, Name, field1__r.Name  FROM Table2');
    for(sObject sobj: objList) {
     
       String ID = sObj.get('id')+'';
       String Name = sObj.get('name')+'';
       // Here i am requiring the code to fectch parent table(i.e Table1) data


}   

 

 

I dont know the syntax to fetch the parent table data, kindly let me know about the required code

 

          

Starz26Starz26

All you are querying in that query is the name of the parent object record.

 

What are you trying to get from the parent object?

 

In your query you would access it like this:

 

objlist.field1__r.Name

DipakDipak

Hi,

 

I know, objlist.field1__r.Name is  used to fetch data of parent table in the Query. But the Query returns a list of SObjects.

 

From that Sobject how will i retrieve the Parent table field data?

 

For Example

in my previous mentioned code

 

String Name = sObj.get('name')+'';

 

 

This statement retrieve the field value of child table.

I don't know how to retrive the field value  of parent table from Sobject

 

Please help me

Nick00000Nick00000

I'm guessing you have to do something like sObj.getSObject('field1__r').get('Name')

 

but its obviously easier if you work with a List of actual objects instead of generic sobjects