0

I have a SOQL build as following:

SELECT Subject, IsChild, StartDateTime, Owner.UserName, Owner.Email, Notes_Universal_ID__c, Location, IsPrivate, IsDeleted, Id, EndDateTime, Description, ShowAs, (SELECT Id, Status, Response, EventId, RelationId, Relation.Email FROM EventRelations) FROM Event where Subject = 'Test Meeting 3' and IsChild = false

In the same query I am looking for performing an additional check Sync_contacts_and_cal_with_Notes__c = true from the User object for both Owner and Attendees.

Is it possible, can someone please help me with the query?

Thanks!

Abhi
  • 303
  • 2
  • 5
  • 13

1 Answers1

1

This is going to get a bit tricky.

Basic idea is simple, just add 2 WHERE clauses. (I'm skipping most of your fields so the change is more visible):

SELECT Subject, Owner.UserName, Owner.Email,
    (SELECT Status, Response, RelationId, Relation.Email 
    FROM EventRelations
    WHERE Relation.Sync_contacts_and_cal_with_Notes__c = true)
FROM Event 
WHERE Subject = 'Test Meeting 3' AND IsChild = false
    AND Owner.Sync_contacts_and_cal_with_Notes__c = true

However this will filter on both tables.

2 users: "Alice" doesn't have the checkbox checked, "Bob" has.

If you have an event owned by "Alice" - all her events won't be synced even though "Bob" is one of attendees on some of them and he'd like to see them!


One way would be to simply include the flag in SELECT instead and manually filter through your records in the code.

Another option is to reverse the relation, start looking from the EventAttendee up and have an OR filter:

SELECT Status, RelationId, RelationEmail, 
    Event.OwnerId, Event.Owner.Email
FROM EventAttendee
WHERE Owner.Sync_contacts_and_cal_with_Notes__c = true
    OR Event.Owner.Sync_contacts_and_cal_with_Notes__c = true
ORDER BY Event

This is better but not ideal as now we'll never see Events that don't have any attendees...


EDIT to answer comments:

  1. You'll see EventAttendees or EventRelations depending on the version of API you're using, I thought we've covered that in previous question? Just substitute the table name and field name accordingly.

  2. Regarding the error - really weird! It seems to accept standard User fields happily but gets stupid when you start using custom ones. One way of solving it would be to contact SF support and opt-in for "polymorphic SOQL" (How to get Email from Task object record using SOQL).

Another way - you'd have to split it into 2 queries, first fetch ids of Users with the checkbox selected, then use them in WHERE OwnerId IN :(set of these id's here).

That, or:

SELECT Subject, Owner.UserName, Owner.Email,
    (SELECT Status, Response, RelationId, Relation.Email 
    FROM EventRelations
    WHERE RelationId IN (SELECT Id FROM User WHERE Checkbox__c = true))
FROM Event 
WHERE IsChild = false
    AND OwnerId IN (SELECT Id FROM User WHERE Checkbox__c = true)
  1. If I recall correctly child Events have ReccurenceActivityId set.
Community
  • 1
  • 1
eyescream
  • 18,088
  • 2
  • 34
  • 46
  • EventAttendee table doesn't exist, therefore "..FROM EventAttendee" isn't working. Also the previous query for some reason is giving this error **INVALID_FIELD: AND Owner.Sync_contacts_and_cal_with_Notes__c ^ ERROR at Row:7:Column:9 No such column 'Sync_contacts_and_cal_with_Notes__c' on entity 'Name'. If you are attempting to use a custom field, be sure to append the '__c' after the custom field name. Please reference your WSDL or the describe call for the appropriate names.** – Abhi Jun 14 '13 at 15:38
  • Also, I'm scratching to figure out how is Parent and Child Events related with each other. Thanks for your help again. – Abhi Jun 14 '13 at 15:44
  • The Query **SELECT Subject, Owner.UserName, Owner.Email, (SELECT Status, Response, RelationId, Relation.Email FROM EventRelations WHERE RelationId IN (SELECT Id FROM User WHERE Sync_contacts_and_cal_with_Notes__c = true)) FROM Event WHERE IsChild = false AND OwnerId IN (SELECT Id FROM User WHERE Sync_contacts_and_cal_with_Notes__c = true)** seems right, but when running against force.ide it just hangs there... sorry I'm relatively new to soql – Abhi Jun 14 '13 at 18:59
  • there seems some issue with force.ide, works fine from eclipse. thanks a lot for your help. – Abhi Jun 14 '13 at 19:03
  • 1
    Use https://workbench.developerforce.com for prototyping of SOQL, Eclipse/ Force.com IDE is bit "behind", doesn't always render results nicely even though the syntax is perfectly valid. Or I'm a big fan of "Real Force Explorer" because it can connect to many environments at the same time and has history of SOQL you've run :) – eyescream Jun 14 '13 at 19:12