質問

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!

役に立ちましたか?

解決

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.
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top