You need to sign in to do that
Don't have an account?
tvaughan
In an APEX trigger, how to I add an event to a shared calendar?
We've got a custom object named SE_Request__c that lets people add a request for support from our sales engineering team. A request might be something like "I need a demo of xyz, via webex, to client Foo on 2012/03/02"
What I'm trying to do is write a trigger on the Object to write a new Event to a shared calendar after insert. Our SF admin has set up a shared calendar named "Ops Calendar", but I can't figure out how to get a userId from that name for use in creating a new Event object.
Here's what I have, with a comment showing where I'm stuck...
trigger SE_Request_Calendar_Trigger on SE_Request__c (after insert) { list<event> newEvents = new list<event>(); string salesOpsCalendar = 'Ops Calendar'; for (SE_Request__c seRequest : System.Trigger.new) { try { newEvents.add(new Event( // How do I convert 'Ops Calendar' to an id that owns our shared calendar? OwnerId = ???, ActivityDateTime = seRequest.Date_of_Requested_Demo__c, DurationInMinutes = 60, // Local or on-site Location = seRequest.Type_of_Demo__c, Subject = 'Give a demo of ' + Products_for_Demo__c + ' for ' + seRequest.Requester_Name__c ) ); } catch(Exception e){ system.debug(e); } } insert newEvents; }
The OwnerId of an event placed on a public calendar will be the ID of the calendar. I know this isn't clearly outlined in the documentation, but you can observe this detail for yourself by using the Apex Explorer or the Developer Console.
Edit: But you'll get an Invalid Owner ID exception if you try to create or assign the event using this ID value. As far as I know, you can't clearly assign an event to a calendar through Apex Code.
All Answers
I don't believe you can assign an Event to a calendar, per se, but you'll need to assign the OwnerId a value of a User's Id.
~ Clint
"I don't believe you can assign an Event to a calendar, per se"
That appears to be correct; I don't see any official "calendar" object in the SFDC API or in the schema explorer, so I assume a "shared calendar" is really just some sort of special construct or view in to the list of Events
So, that said, do you know how I get the "ownerId" of a shared calendar named "Ops Calendar"?
The OwnerId of an event placed on a public calendar will be the ID of the calendar. I know this isn't clearly outlined in the documentation, but you can observe this detail for yourself by using the Apex Explorer or the Developer Console.
Edit: But you'll get an Invalid Owner ID exception if you try to create or assign the event using this ID value. As far as I know, you can't clearly assign an event to a calendar through Apex Code.
Thanks sfdcfox. I was poking around high & low trying to figure out how to do this and it's great to get a second opinion.
I appreciate the alternative suggestions too, clint. Thanks for the replies.
Hello All, the documentation has been updated and can be found here (https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_event.htm?search_text=public%20calendar)
Specifically this section:
OwnerIdType
reference
Properties
Create, Defaulted on create, Filter, Group, Sort, Update
Description
Contains the ID of the user or public calendar who owns the event. Label is Assigned to ID.
I've been able to automate event creation on a public calendar only using flow.
the CalendarId is just a text field holding the ID of my public calendar.
This should resolve your issue while not being done in APEX