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
louisa barrett 7louisa barrett 7 

didn't understand relationship Project_Checklist__r in FROM part of query

Hi,

I have two custom objects Project_sheet_2 and Project_checklist, they both have a look up to Opportunity.
On the Poject_sheet_2 VF page I would like to display all the associated Project_checklist objects and vice versa.
I'm holding the Opporutnity ID in a variable(projSheet.PS2_Opportunity__c) as I'm also displaying the Opportunity Product Items.
I have the following code on my Project Sheet controller, which I thought should work.....

public List<Project_Checklist__c> getOppChecklists(){
        List<Project_Checklist__c> lstChecklists = [SELECT Name,(SELECT name FROM Poject_checklist__R) FROM Opportunity WHERE ID =:projSheet.PS2_Opportunity__c];      
        return lstChecklists;
}

Many thanks
Best Answer chosen by louisa barrett 7
Santosh Kumar 275Santosh Kumar 275
For parent-to-child relationships, the parent object has a name for the child relationship that is unique to the parent, the plural of the child object name. For example Suppose we have two objects Parent__c and Child__c(Being related as LookUp or MasterDetail).
So to return the last name of all Parent, and for each Parent returned, the last name of the parent's child we will use this query.
SELECT LastName__c,
  (
    SELECT LastName__c
    FROM Child__r
  )
FROM Parent__c

Here in your case Project_Checklist__r  represents the relationship name.
It will return Name of all the oppurtunity and for each oppurtunity returned, the name of realted Project_checklist.

For more detail refer this link : https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_and_custom_objects.htm

Hope you got the solution.
Mark this answer as Best Answer if this help you.
 

All Answers

louisa barrett 7louisa barrett 7
I forgot to mention, the Project_sheet_2 object has no look up to the Project_checklist object, they are only indirectly linked by the Opportunity.
Santosh Kumar 275Santosh Kumar 275
For parent-to-child relationships, the parent object has a name for the child relationship that is unique to the parent, the plural of the child object name. For example Suppose we have two objects Parent__c and Child__c(Being related as LookUp or MasterDetail).
So to return the last name of all Parent, and for each Parent returned, the last name of the parent's child we will use this query.
SELECT LastName__c,
  (
    SELECT LastName__c
    FROM Child__r
  )
FROM Parent__c

Here in your case Project_Checklist__r  represents the relationship name.
It will return Name of all the oppurtunity and for each oppurtunity returned, the name of realted Project_checklist.

For more detail refer this link : https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_relationships_and_custom_objects.htm

Hope you got the solution.
Mark this answer as Best Answer if this help you.
 
This was selected as the best answer