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
Abhishek Singh 88Abhishek Singh 88 

how to access one objects records using junction object when we have records of another custom object

User-added image
i have these three object as account and client Link__c is a junction object.
now i have to find details of Client__c where i have record account.customerid__c.
query i have written is. 
clientLst=[select Name from Client__c where client__c.id IN:(select Account__r.CustomerId__c from Accounts_Users_Link__r where Account__r.CustomerId__c=:currentSiteCust)];
where " Accounts_Users_Link__r "is child relation ship name
I am getting error unexpected token 'select'
Thanks in advance.
Best Answer chosen by Abhishek Singh 88
Vigneshwaran GurunathanVigneshwaran Gurunathan
clientLst=[select Name from Client__c where id IN:(select User__c from Accounts_Users_Link__c where Account__r.CustomerId__c=:currentSiteCust)];
Explanation:

1. changed client__c.id to id
2. You should return the id of the object being queried from the subquery in where condition of main query. So changed Account__r.CustomerId__c to User__c(after looking your diagram with field name User)
3. changed Accounts_Users_Link__r to Accounts_Users_Link__c as it should be used with the API name in subquery in where condition.

Hope it helps.
 

All Answers

Vigneshwaran GurunathanVigneshwaran Gurunathan
clientLst=[select Name from Client__c where id IN:(select User__c from Accounts_Users_Link__c where Account__r.CustomerId__c=:currentSiteCust)];
Explanation:

1. changed client__c.id to id
2. You should return the id of the object being queried from the subquery in where condition of main query. So changed Account__r.CustomerId__c to User__c(after looking your diagram with field name User)
3. changed Accounts_Users_Link__r to Accounts_Users_Link__c as it should be used with the API name in subquery in where condition.

Hope it helps.
 
This was selected as the best answer
Abhishek Singh 88Abhishek Singh 88
Hi Vigneshwaran
thanks for your reply,But it is still showin same error" unexpected token :select"
Vigneshwaran GurunathanVigneshwaran Gurunathan
Is this SOQL query inside apex class/apex trigger or you executing this in Query editor of Developer Console?
Abhishek Singh 88Abhishek Singh 88
it is in apex class
Abhishek Singh 88Abhishek Singh 88
Just Replaced colon before subquery and it works.Thanks Vigneshwaran