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
tvaughantvaughan 

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;
}

 

 

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

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

ClintLeeClintLee

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

 

tvaughantvaughan

"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"?

ClintLeeClintLee
A shared calendar is really just a custom view filtered by some criteria. The view doesn't have an owner. You might try creating the event and assigning it to someone on the ops team, then use the EventAttendee object to add the rest of the ops team as attendees. You could also think about creating a Queue for your SE Request object and add the ops team to the queue. Then, assign all new records to the queue. Views could then be created on the SE Request object for the queue showing things like Today's Requests, Requests This Week, etc. Hope that helps. ~ Clint
sfdcfoxsfdcfox

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.

This was selected as the best answer
tvaughantvaughan

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.

Owen ParkOwen Park
@tvaughan did you get a resolution for this? I am looking to do a similar thing within my organisation and this is still not supported through Lightning Process Builder.
Brett Thompson 20Brett Thompson 20

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.
User-added image

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