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
pooja biswaspooja biswas 

parent-child & child-parent soql query custom object

Hi
I have university as parent object & college as child object.
There is a master-detail relationship defined on child object i.e college

I have a parent-child soql as follows
List<University__c> univList=[select Name,(select Name from colleges__r) from university__c];
system.debug(univList);

Also I have a child-parent soql as follows
List<college__c> univList=[select Name,university__r.Name from college__c
system.debug(univList);

when I execute the parent-child query I do not get any child related records .
Also when I execute child-parent query I do get the name of the college but the university ID I get but I want the university name also not the ID

please let me know.

Thanks
pooja
 
Best Answer chosen by pooja biswas
Banwari kevat1Banwari kevat1
Hi pooja,
   Please try this piece of code in developer console. I think you would get what tou want.
For parent-child soql
for(University__c u: [select Name,(select Name from colleges__r) from university__c])
{   
    for(College__c c:u.Colleges__r)
         System.debug('University Name:'+u.Name+'   College Name:'+c.Name);
}
This code will give you all universities name and colleges under university


And
For child-Parent soql
For(College__c c:[select Name,university__r.Name,university__c from college__c])
{
  system.debug('College Name:'+c.Name+'     University Name:'+c.university__r.Name);
}
This code will give you all colleges name with respected university.

Please choose best answer if your problem has been resolved.

Regards,
Banwari K



 

All Answers

sfdcMonkey.comsfdcMonkey.com
hi pooja
try this onces
parent-child soql as follows
List<University__c> univList=[select Name,(select Name from colleges__c) from university__c];
system.debug(univList);
Priyanka S 27Priyanka S 27
Hi Pooja,

There are 2 things to consider before you decide which type of query to use,
1) Parent records must have atleast one or more child records.
       Use child to parent relationship query
2) Parent records may or may not have child records
       Use parent to child relationship query

Make sure that you have checked the child relationship name for university__c object. In child-parent soql the field university__r.Name might be the autonumber. Please do check whether the university name is stored on any other field on the university__c record. If so use that field in the relationship query.

Thanks,
Priyanka S

 
pooja biswaspooja biswas
Hi priyanka
Parent record has more than 2 child records.
so while using child to parent query, the field Name in parent object is Text, it is not Auto Number, it is stored in Name field only.

Thanks
pooja B


 
pooja biswaspooja biswas
Hi piyush
I tried ur query but it doesnt work.

Thanks
pooja
Banwari kevat1Banwari kevat1
Hi pooja,
 Your query is working fine in my org. May your test data you have created is not be proper. Please let me know your test data.User-added image

User-added image

Please let me know if any confusion.

Regards
Banwari kevat
pooja biswaspooja biswas
Hi Banwarlal
Here is my test data for parent to child query


creating parent object record

creating child record

User-added image

Now when I run the query: 
List<university__c> univList=[select Name,(select Name from colleges__r) from university__c];
system.debug('--univList--'+univList);

I am getting like this in developer console as follows:
developer console result
I hope I am clear.
Please tell me where I am going wrong.

Thanks
Pooja Biswas
 
Priyanka S 27Priyanka S 27
Hi pooja,

Looks like there is no error on your code. Can you try the same code in the anonymous apex and  print the log. 

Let me know if you could get the result.

Thanks,
Priyanka S
Banwari kevat1Banwari kevat1
Hi pooja,
   Please try this piece of code in developer console. I think you would get what tou want.
For parent-child soql
for(University__c u: [select Name,(select Name from colleges__r) from university__c])
{   
    for(College__c c:u.Colleges__r)
         System.debug('University Name:'+u.Name+'   College Name:'+c.Name);
}
This code will give you all universities name and colleges under university


And
For child-Parent soql
For(College__c c:[select Name,university__r.Name,university__c from college__c])
{
  system.debug('College Name:'+c.Name+'     University Name:'+c.university__r.Name);
}
This code will give you all colleges name with respected university.

Please choose best answer if your problem has been resolved.

Regards,
Banwari K



 
This was selected as the best answer
pooja biswaspooja biswas
Hi Banwari
That worked. Thanks for updating my knowledge.

Thanks
Pooja B
Soene IbaniboSoene Ibanibo
Pls i'm new to SOQL and Visualforce can someone help me correct my code cos  i have been able to display  a record on pdf format but it doesnt match the Record Id and Account the pdf needs to generate from. see code below
public class contractorController {
  
  

   public Contractor_LPO__c purchaseList {get;set;}
   public string recordId {get;set;}
   
   public contractorController ()

   {
       recordId = Apexpages.currentPage().getParameters().get('Id');
       purchaseList =new Contractor_LPO__c();

       purchaseList = [select Id, Contractor_Quote__r.Contractor_Account__c, Contractor_Quote__r.id , Contractor_Quote__r.Amount_in_Words__c, Contractor_LPO__c.Name, Amount_in_Words__c, Contractor_Quote_Approved_Amount__c, Contractor_Quote_Status__c, Contractor_RFQ_Number__c, LPO_Date__c, Payment_Terms_and_Condition__c, Quantity__c, Quote_Description__c , Contractor_Quote__c,Status__c, Tax_Inclusive_Amount__c,Value_Added_Tax__c , VAT_Inclusive__c, Contractor_Account__c 
        from Contractor_LPO__c limit 1]; 

   }

}
Hrishikesh PednekarHrishikesh Pednekar
Parent Custom Object Label :- Company
Parent Custom Object Plural Label :- Companies
Parent Custom Object Name :- Company__c
Child Custom Object Label :- Guest House
Child Custom Object Plural Label :- Guest_Houses
Child Custom Object Name :- Object_Guest_House__c
Write query on the parent object to query the associated child records also.
Ans. Select Id, Name, (Select Id, Name from Guest_Houses__r) from Company__c
The query will use the Child Custom Object Plural Label.
KkngKkng
Hi, This is regarding child to Parent query and prefill the some of the parent fields in Child when a user tries to create a new record.
Say I have Custom Employee__c obj which has employees info like Fname, Lname, EmpId, OfcLocation etc.. 
Now I have created a new custom object Myapp_Emp__c which needs Employee__c object fields information and some additional information like IsWFH, IsLicensed etc,.
So, I have created these new additional fields on Myapp_Emp__c  and lookup to Parent Employee__c obj.

I am going to create new VF for NEW button of Myapp_Emp__c  which opens a page with these parent fields Prefilled for that User, and Additional fields to Enter data.
It is Based on User who is already a Employee so, Getting the prefilled fields like Fname, Lname, EmpId, OfcLocation etc,. from Parent here.

please clarify:
1. Do I need to create the fields which i needed from Parent again on new child Object. ie, Fname, Lname etc,. on Child object. or just a lookup is enough.

2. How to write controller and VF for this page.
 
Appriate your response.
Umar Mohammad 12Umar Mohammad 12
Hello Banwari,

I need your help for the same scenario. I have a parent object CTSS_Study__c and child object CTSS_Sourcing_Decision__c . I have field CTSS_Classifications__c in child object i.e Sourcing decision.

What I want is this classification to be displayed as one of the columns on VF along with all the data in parent object . i. e Study.

My code if 

public class CTSS_latestVersionStudyReport{
   public   List<CTSS_Study__c> getLatestversion(){
      List<CTSS_Study__c> Latestversion = [Select Name, Status__c, Version__c,(SELECT CTSS_Classifications__c FROM Sourcing_Decision__r) FROM CTSS_Study__c ];
       return Latestversion;
    }
}

What I want is : a classification column (from child object) along with other filed columns of parent object as shown in Pic. Just need classification column to be added in the following table. 

Could you please help me with the code. I am sure it will work with the approach you have given to Pooja. need your inputs please.



 User-added image
Hira ZaidiHira Zaidi
Hi am Hira m fresher in salesforce 

I would ask can we use aggregate list  with related dml??????
Akshay Jadhav 19Akshay Jadhav 19
I have two objects  Company as parent and Employee as a child but whenever I tried to access child data from the parent  it throws an error as follows:

select Name,(select Name from Employee__r) from Company__c ^ ERROR at Row:1:Column:31 Didn't understand relationship 'Employee__r' in FROM part of query call. If you are attempting to use a custom relationship, be sure to append the '__r' after the custom relationship name. Please reference your WSDL or the description call for the appropriate names.

how to overcome it?
Preethi Reddy 3Preethi Reddy 3
You need to use Employees__r -- plural label