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
Tracy BinghamTracy Bingham 

My first ever SOQL query? Works until I try and get the user name.

Where am I going wrong?  this works if I only ask for the Owner ID but as soon as I try and get the user name I get an unknown error parsing query.

SELECT OwnerId,  User.Name FROM Event, StartDateTime,Subject,Hotel__c,Activity__c,Notes__c,    Done__c,Completed__c,Cancelled__c,Postponed__c
FROM Event
WHERE StartDateTime = LAST_MONTH

Any help much appreciated.

Thanks
Best Answer chosen by Tracy Bingham
Shri RajShri Raj
I see few issues 

The User.Name field is not related to the Event object. You need to join the Event object with the User object using a relationship field, such as OwnerId.
You have multiple FROM statements in your query, which is not allowed. You can only have one FROM statement in a SOQL query, and it should specify the object you are querying.
The list of fields after the FROM statement should not include StartDateTime, Subject, Hotel__c, etc. Those fields should be included in the list of fields after the SELECT statement.
Here is a corrected version of your SOQL query:
SELECT OwnerId, Owner.Name, StartDateTime, Subject, Hotel_c, Activityc, Notesc, Donec, Completedc, Cancelledc, Postponed_c
FROM Event
WHERE StartDateTime = LAST_MONTH



This query selects the OwnerId and Owner.Name fields from the Event object, and filters the results to show only events that have a StartDateTime value in the last month.

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Tracy,

User field is not available in event object, you can remove the  User.Name from query and try.
Refer the below object reference link
https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_event.htm

If this helps, Please mark it as best answer.

Thanks!!
Tracy BinghamTracy Bingham
If I remove the "User.name FROM Event" from the query it works.  However I need the user name and the user ID, is there any way of getting both using a SOQL query on Event?    The SOQL query trailhead for admins makes it seem as though this would be possible but only gives examples for Accounts and contacts. And your documentation link only suggests the ID is available to query.  Thanks so much for your help.
Shri RajShri Raj
I see few issues 

The User.Name field is not related to the Event object. You need to join the Event object with the User object using a relationship field, such as OwnerId.
You have multiple FROM statements in your query, which is not allowed. You can only have one FROM statement in a SOQL query, and it should specify the object you are querying.
The list of fields after the FROM statement should not include StartDateTime, Subject, Hotel__c, etc. Those fields should be included in the list of fields after the SELECT statement.
Here is a corrected version of your SOQL query:
SELECT OwnerId, Owner.Name, StartDateTime, Subject, Hotel_c, Activityc, Notesc, Donec, Completedc, Cancelledc, Postponed_c
FROM Event
WHERE StartDateTime = LAST_MONTH



This query selects the OwnerId and Owner.Name fields from the Event object, and filters the results to show only events that have a StartDateTime value in the last month.
This was selected as the best answer
Shri RajShri Raj
Yes, you can retrieve both the OwnerId and the Owner.Name fields in the same query. To do this, you need to join the Event object with the User object using the OwnerId field. Here's the corrected query:
 
SELECT OwnerId, Owner.Name, StartDateTime, Subject, Hotel__c, Activity__c, Notes__c, Done__c, Completed__c, Cancelled__c, Postponed__c
FROM Event
WHERE StartDateTime = LAST_MONTH

In this query, the Owner.Name field is related to the Event object through the OwnerId field, which is a lookup relationship to the User object. This allows you to retrieve both the OwnerId and the Owner.Name fields in the same query
Tracy BinghamTracy Bingham
Thanks so much, @Shri Raj this totally solved my problem, and thank you too for the explanation as I also now understand where I was going wrong.  Can't thank you enough.  T