You need to sign in to do that
Don't have an account?
Wm Peck 1958
how to do a basic join in SOQL with WHERE clause on related table
I want to do a "left join" of CONTACT table to TargetX_SRMb__Application__c, but I want to filter the Application table by "Class_Year = '2021'
and can't seem to get that.
I can do a successful query like this but ALL rows are returned, so it's ignoring the WHERE clause in my inline (SELECT ...)
I'm trying to do this, logically:
(
select [this and that],
(select [other stuff] from Table_B)
from Table_A
)
where Table_B_field = '2021'
I can do this in Oracle with a snap, but how to do so in Salesforce / SOQL / Workbench.
and can't seem to get that.
I can do a successful query like this but ALL rows are returned, so it's ignoring the WHERE clause in my inline (SELECT ...)
select ais_candidate_number__c, AIS_Record_Type_Name__c, TargetX_SRMb__Status__c, FirstName, LastName, birthdate, TargetX_SRMb__Gender__c, (select TargetX_SRMb__Application_Decision__c, TargetX_SRMb__Start_Term_Year__c, AIS_Feeder_Source__c from TargetX_SRMb__Application__r where TargetX_SRMb__Start_Term_Year__c = '2021') from Contact where AIS_Record_Type_Name__c = 'Recruitment_Manager_Student' and TargetX_SRMb__Application__r.TargetX_SRMb__Start_Term_Year__c = '2020'But if I flip the query I can't get it to work ... CONTACT__r doesn't work ...
select name, recordtypeid, targetx_srmb__contact__c, targetx_srmb__application_decision__c, targetx_srmb__start_term_year__c, (select AIS_PRMI_Id__c, ais_candidate_number__c, AIS_Record_Type_Name__c, TargetX_SRMb__Status__c, FirstName, LastName, birthdate, TargetX_SRMb__Gender__c, usna_international__c from Contact__r ) from TargetX_SRMb__Application__c where targetx_srmb__start_term_year__c = '2021'Just learning SOQL so it's a challenge.
I'm trying to do this, logically:
(
select [this and that],
(select [other stuff] from Table_B)
from Table_A
)
where Table_B_field = '2021'
I can do this in Oracle with a snap, but how to do so in Salesforce / SOQL / Workbench.
*** No idea what Topic to put this under ...
If this object has a lookup relationship to Contact, then by correcting the query, it should work.
You've added the field after FROM in the SOQL, which isn't the right way.
Try the below query please
This should work if there's a Contact__c field existing on your TargetX_SRMb__Application__c object.
All Answers
Hi there,
In your flipped query, how you'd access the Contact__r fields is like this
select name, recordtypeid, targetx_srmb__contact__c, targetx_srmb__application_decision__c, targetx_srmb__start_term_year__c, Contact__r.AIS_PRMI_Id__c, Contact__r.ais_candidate_number__c, Contact__r.xx from TargetX_SRMb__Application__c
This way you'll be able to query data from the related contact record.
And also, your query 1 should work.. when you put a filter inside the inner query, the related records that match the filter will only be returned.
Since we don't have joins in salesforce, what we have is relationship queries
Child to parent query
Standard objects : ex: parent:Account, child: related Contacts
Select Id, LastName, Account.Name FROM Contact
Custom objects : ex parent : Class__c, child : related Students (Student__c)
Select Name, Class__r.Name FROM Student__c WHERE xx
Parent to child query
Standard objects : ex: parent:Account, child: related Contacts
Select Id, Name, (Select LastName FROM Contacts) FROM Account
Custom objects : ex parent : Class__c, child : related Students (Student__c)
Select Name, (Select Name FROM Student__r) FROM Class__c
I hope this helps.
You can find more detailed documentation here - https://developer.salesforce.com/blogs/developer-relations/2013/05/basic-soql-relationship-queries.html
query works fine without the "Contact__r" reference / field.
If this object has a lookup relationship to Contact, then by correcting the query, it should work.
You've added the field after FROM in the SOQL, which isn't the right way.
Try the below query please
This should work if there's a Contact__c field existing on your TargetX_SRMb__Application__c object.
This worked, which I got from a colleague* *I needed to do my homework before contacting a colleague.
There's a lot I don't understand, such as there's no object named TargetX_SRMb__Contact__c but somehow that is used to get CONTACT info.
I'm good for this question, thanks for the assist, it get me started ...