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
Namit PalNamit Pal 

Unable to write an SOQL Query for the junction Object?

Hi everyone. I have a junction object called Project Mapping which has a lookup to a custom object- 'Project' and a lookup to the Standard Object- 'User'. The junction object is the link between the two objects which is mapping the objects.  

My objective is to create a SOQL query to retrieve the Projects related to a particular user, specifically the user who has signed in. 

I am struggling a lot to create that query. Can someone please help me with that query? 


User-added image

Rahul KumarRahul Kumar (Salesforce Developers) 
Hi Namit,

May I suggest you please refer the below link for reference. Hope it will be helpful.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar
 
Malni Chandrasekaran 2Malni Chandrasekaran 2
Namit,
If you just need project details, you may try the below one and let me know

 SELECT
     Project, Project__r.Project_Name, Project__r.StartDate -- May add whatever fields you need from Project object.
FROM
    Project_Mapping__c
WHERE
    User =:UserInfo.getUserId()  --Assume User field in Project Mapping junction object is the User ID let me know if not .
Glyn Anderson 3Glyn Anderson 3
You need to use a semi-join in your SOQL query.  The query below returns all projects that are looked up to by junctions that look up to the current user.

<pre>
List<Project__c> projects =
[   SELECT  Id, // other fields here
    FROM    Project__c
    WHERE   Id IN (SELECT Project__c FROM Project_Mapping__c WHERE User__c = :UserInfo.getUserId())
];
</pre>
 
Glyn Anderson 3Glyn Anderson 3
Namit,  Did any of these answers solve your problem?  If so, please mark the questions as "Solved".  Many others will have a question similar to yours, as junction objects are a fundamental concept.  Thanks!