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
Ryan Thomas 317Ryan Thomas 317 

soql Events - "shared activities"

Can someone help me with a query to get the following:

- OwnerId
- Event ID
- ActivityDate
- Subject
- Opportunity ID
- Contact ID (invitees/participants)
 
Raj VakatiRaj Vakati
Try thi s
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_openactivity.htm?search_text=Activity
 
Select Id , ActivityDate,OwnerId,Subject,WhatId,WhoId form OpenActivity

 
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
Ryan, There are a few ways to construct the query depending on exactly what you're doing with the results.  My example below is just one possible solution.  Note that Event does not have an OpportunityId field; but if the Event is related to an Opportunity, that Id will be in the WhatId field.  Also, the RelationId field of the EventRelation record will contain the Id of a Lead, Contact or User.  If that person is an invitee, then IsInvitee will be true.  You can add WHERE clauses as needed.

<pre>
List<Event> events =
(   [   SELECT  Id, OwnerId, ActivityDate, ActivityDateTime, Subject, WhatId,
            (   SELECT  RelationId, IsInvitee, IsParent, Status
                FROM    EventRelations
            )
        FROM    Event
    ]
);
</pre>
Ryan Thomas 317Ryan Thomas 317
Thanks Glyn
Ryan Thomas 317Ryan Thomas 317
This doesn't list the participants on a separate line.  Is there a way to do this or can you help with the batch job to copy these to OCR?
Ryan Thomas 317Ryan Thomas 317
Hi Glyn.  Any suggestions on how to pull this off or possibly a place I can go to for help?
Glyn Anderson (Slalom)Glyn Anderson (Slalom)
I'm not sure exactly what you're trying to do.  The query will return the data, but I don't know the context you're using this in.  Are you going to list the participants in a Visualforce page?  Lightning component?  In an email?  Help me understand, and I'll try to help you out.
Ryan Thomas 317Ryan Thomas 317
Thanks Glyn.  If we add an Event to an opportunity, we'd like to copy the Participants/Invitees to the Contact Roles object if they do not already exist (if not OCR than we could copy to a custom object).  We do have Shared Activities turned on so we typically have multiple contacts on the Event.  I know there are restrictions with OCR, so maybe a batch job at midnight if a trigger is not possible and/or copying to another custom object (Application Contacts).  Your query above works and is helpful but I can't figure out how to loop through the list of all the contacts and relate to that particular Opportunity and add them to OCR.

Ultimately looking for something similar to the solution on this question but copying the contacts to OCR so that this batch job runs at night:
https://developer.salesforce.com/forums/ForumsMain?id=9060G0000005j5gQAA

Let me know if you can help.

Have a nice weekend!