+ Start a Discussion
nitin sharmanitin sharma 

Relationship queries involving custom objects

Hi,

Below given is my code and it is not the complete  .

I have Reservation object and invoice .When somebody registers with us ,Invoice for that customer should be automatically created as child object.

I have lookup relationship beyween invoice and Reservation.

I want to get all the invoices associate with the particular registration.Since reservation__ is the paretn object and invoice is the child,therefore,i am trying the following query in the code.However,I get the folowing error.

Compile Error: Didn't understand relationship 'reservation__r' in field path. 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 describe call for the appropriate names. at line 166 column 49

Can somebody please help?


list<Reservation__c>pc=new List<Reservation__c>([select id,(select id from reservation__r.invoice__c)from reservation__c where reservation__c in:samp])



 


trigger  reservation on reservarion__c(before delete
{
Map<id,invoice__c>must=new Map<id,invoice__c>();
list<invoice__c>ok=new list<invoice__c>();
If(Trigger.IsBefore & Trigger.IsDelete)
{
//Map<id,invoice__c>must=new Map<id,invoice__c>();
//list<invoice__c>ok=new list<invoice__c>();
Set<id> samp=new set<id>();
For(reservation__c r:trigger.old)
{

samp.add(r.id);
System.debug('The values in the record of delete trigger of the samp is##########################'+samp);
}

list<Reservation__c>pc=new List<Reservation__c>([select id,(select id from reservation__r.invoice__c)from reservation__c where reservation__c in:samp])
}
Best Answer chosen by nitin sharma
Subhash GarhwalSubhash Garhwal
Hi Nitin sharma,

In your query you need to use Child Relationship Name for child object and after that use __r
Like if Invoice is child of Reservation__c and if Child Relationship Name of Invoice is Invoices that Use your query Like

List<Reservation__c>pc=new List<Reservation__c>([select id, (select id from Child Relationship Invoices__r) from Reservation__c where Id IN : samp])

To find out Child Relationship Name follow these steps

Custom Object -> Invoice -> Lookup Field (Reservation__c) -> Child Relationship Name

Thanks

All Answers

Mani RenusMani Renus
I'm not sure but in query instead of 'reservation__r'  check out with 'reservations__r'
Subhash GarhwalSubhash Garhwal
Hi Nitin sharma,

In your query you need to use Child Relationship Name for child object and after that use __r
Like if Invoice is child of Reservation__c and if Child Relationship Name of Invoice is Invoices that Use your query Like

List<Reservation__c>pc=new List<Reservation__c>([select id, (select id from Child Relationship Invoices__r) from Reservation__c where Id IN : samp])

To find out Child Relationship Name follow these steps

Custom Object -> Invoice -> Lookup Field (Reservation__c) -> Child Relationship Name

Thanks
This was selected as the best answer
nitin sharmanitin sharma
Thanks for you reply,

It did not help:- I get the following error.  I know that it cannot be reservations__r.invoice__c.It is not correct.I left it by mistake.

The idea is that when we use parent to child iqueries in standard objects then we use relationship field names

For example,

Select id,(select id from contacts) from account

In the abobe quuery we are specifying comtacts not contact.There is something about custom object.I am not sure how to traverse with custom objects.

I looked at relationship queries.I am still not able to figure out.

Error: Compile Error: Didn't understand relationship 'reservations__r' in field path. 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 describe call for the appropriate names. at line 167 column 49
Subhash GarhwalSubhash Garhwal
Hi Nitin sharma,

Can you please provide query in which you getting error.
Ramesh KallooriRamesh Kalloori
try the below.

https://success.salesforce.com/answers?id=90630000000hvV7AAI

http://www.salesforce.com/us/developer/docs/dbcom_soql_sosl/Content/sforce_api_calls_soql_relationships.htm

thanks,
RAmesh
nitin sharmanitin sharma
Thanks Ramesh,Subhash  and Mani 

Hi Subhash ,

I have got the query right.Now ,how do I loop through child objects.I am struggling with the syntx.Can you please help me in setting things right in the inner for loop.

Child relationship name is Invoices

List<reservation__c>lope=([Select id,(select id from Invoices__r) from reservation__c where id in:samp]);

for(reservation__c clever:lope)
{

   For( Invoice__c:clever.invoices__r )
           {


           }
}
nitin sharmanitin sharma
H All,

It got resolved.

Thanks to all for your woderful support.