You need to sign in to do that
Don't have an account?
pooja 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
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
Please try this piece of code in developer console. I think you would get what tou want.
For parent-child soql This code will give you all universities name and colleges under university
And
For child-Parent soql 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
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);
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
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
I tried ur query but it doesnt work.
Thanks
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.
Please let me know if any confusion.
Regards
Banwari kevat
Here is my test data for parent to child query
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:
I hope I am clear.
Please tell me where I am going wrong.
Thanks
Pooja Biswas
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,
Please try this piece of code in developer console. I think you would get what tou want.
For parent-child soql This code will give you all universities name and colleges under university
And
For child-Parent soql This code will give you all colleges name with respected university.
Please choose best answer if your problem has been resolved.
Regards,
Banwari K
That worked. Thanks for updating my knowledge.
Thanks
Pooja B
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.
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.
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.
I would ask can we use aggregate list with related dml??????
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?