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
Naveed AnwarNaveed Anwar 

SOQL for multilevel relationships for custom objects

 Hi,

 

I am trying to build a query for multilevel relations.

 

I have 3 Objects that are involved in the query.

Item, PackageItem and Package.

 

Item linked to PackageItem through ItemCode.

PackageItem linked to Package through PackageCode.

 

What i want is to extract "PackageCode". So far i tried the following query.

 

Select Name, (Select Name From PackageItems__r) From Item__c Where ProgramID__c != null

 

I cannot find a way to include Package object in the query.

 

Please help

Thanks!

 

Best Answer chosen by Admin (Salesforce Developers) 
Naveed AnwarNaveed Anwar

Hi,

 

I got the solution.

 

When i expanded the Package__c reference field in custom object PackageItems__c  using "sforce explorer" i found the Package__r relation.

 

Select Name, (Select Name, Package__r.Name From PackageItems__r)
From Item__c Where ProgramID__c != null

 

Thanks for your help.

All Answers

Going South.ax738Going South.ax738

From the API documentation on relationships:

 

  • In each specified relationship, only one level of parent-to-child relationship can be specified in a query. For example, if the FROM clause specified Account, the SELECT clause could only specify the Contact or other objects at that level. It could not specify a child object of Contact.

You can create a dummy formula column on package item for package name [say 'mypkgname'] and use it as a column from packageitem__r as

 

Select Name, PackageItems__r.name, PackageItems__r.mypkgname From Item__c Where ProgramID__c != null

Naveed AnwarNaveed Anwar

Hi,

 

When i try to run the following query:

 

Select Name, PackageItems__r.Name From Item__c Where ProgramID__c != null

 

i got the error:  Didn't understand relationship PackageItems__r in field path ......

 

The query that i mentioned in my first post runs fine.

 

Can you give me some workaround for the above error?

 

Thanks!

Naveed AnwarNaveed Anwar

Hi,

 

I got the solution.

 

When i expanded the Package__c reference field in custom object PackageItems__c  using "sforce explorer" i found the Package__r relation.

 

Select Name, (Select Name, Package__r.Name From PackageItems__r)
From Item__c Where ProgramID__c != null

 

Thanks for your help.

This was selected as the best answer