+ Start a Discussion
Diane RoyerDiane Royer 

SOQL Query Giving Error Message

I'm trying to write an SOQL query that will pull information from the Contact object and the Event Object.  When I tried to use Event as the child object, I received an error message that Event could not be the child, so I switched the query around to this:
SELECT Event.whoid, Event.StartDateTime, Event.Subject, (select contact.id, contact.firstname, contact.lastname, contact.MailingStreet, contact.MailingCity, contact.MailingState, contact.MailingPostalCode from Contact)
from Event
Where Event.whoid = '0034M000020OgliQAC'

Unfortunately, it still isn't working.  It tells me "Didn't understand relationship 'Contact' in FROM part of query call.".  What am I doing wrong?

In SQL, the query would look like this:
Select Event.Whoid, Event.StartDateTime, Event.Subject, Contact.firstname, contact.lastname, contact.MailingStreet, Contact.MailingCity, contact.MailingState, contact.MailingPostalCode
from Event
Join Contact on Event.Whoid = Contact.id
where event.whoid = '0034M000020OgliQAC'

Any assistance would be greatly appreciated.
Best Answer chosen by Diane Royer
Abdul KhatriAbdul Khatri
You are missing a ',' after Subject field in the second line. Use this 
SELECT 
    WhoId, StartDateTime, Subject,
    TYPEOF Who
        WHEN Contact THEN Firstname, Lastname, MailingStreet, MailingCity, MailingState, MailingPostalCode
    END
FROM Event
WHERE WhoID = '0034M000020OgliQAC'

 

All Answers

Abdul KhatriAbdul Khatri
Hi Diane,

Event and Task are special kind of SObject, they are not child to Contact. Also the WhoId is either Lead or Contact so Salesforce has introduce TYPEOF feature in SOQL that can help you 
https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_typeof.htm

This is a quick SOQL for your help but I would like to see how you wanted to use it because so provide you with complete solution
SELECT 
    WhoId, StartDateTime, Subject,
    TYPEOF Who
        WHEN Contact THEN Firstname, Lastname, MailingStreet, MailingCity, MailingState, MailingPostalCode
    END
FROM Event
WHERE WhoID = '0034M000020OgliQAC'
I hope this will help you moving forward
Diane RoyerDiane Royer
Thank you very much for the help. 

When I try to run the query you sent, I get an "Unknown error parsing query".

I'm trying to get an SOQL query set up to use in a Conga Mail Merge.  Eventually, I want to get these fields populated in a template letter thanking contacts for coming for a visit.
Abdul KhatriAbdul Khatri
Hi Diane,

If you are trying to use this query in an apex class or anywhere please make sure the api version is either 46 or greater as it is supported only there.
User-added image

If you can share some part of your code, will definitely help you further.
Diane RoyerDiane Royer
Actually, right now, I'm just trying to get it to run in the Developer Console.  I'm not using any apex classes for this project (at least not yet).  Where can I find my API version? 
Abdul KhatriAbdul Khatri
hmm, it should work there. Can you check the what release your org has? If you can provide the screen shot of your org or provide the screen shot where you are getting this message.
User-added image

Work for me good
Diane RoyerDiane Royer
I'm not sure where to look for the release that we have.  We are on instance NA129 Enterprise Edition.  Here is the snapshot of the screen where I am getting the error message.
User-added image
Abdul KhatriAbdul Khatri
You are missing a ',' after Subject field in the second line. Use this 
SELECT 
    WhoId, StartDateTime, Subject,
    TYPEOF Who
        WHEN Contact THEN Firstname, Lastname, MailingStreet, MailingCity, MailingState, MailingPostalCode
    END
FROM Event
WHERE WhoID = '0034M000020OgliQAC'

 
This was selected as the best answer
Abdul KhatriAbdul Khatri
Hi Diane,

Were you able to get my last update? Was this helpful?
Diane RoyerDiane Royer
I just got your last update.  It worked perfectly.  Thank you so much.