The CalendarView
object documentation displays a code sample that shows "sharing" a calendar by assigning ownership to a group, user list, or profile. You can get close to what you hope to achieve by using a group, user list, or profile to give the calendars you want with a large set (or nearly all by using a group like "All Internal Users") of users at one time.
This will make the calendar appear on all of the users' calendar apps automatically which it sounds like is what you're most interested in, even though this is not, strictly speaking, a Salesforce Public Calendar.
CalendarView
example using Group
:
Group userGroup = [
SELECT Id
FROM Group
WHERE Name = 'Sales Group'
LIMIT 1
];
List<Id> groupId = new List<Id>();
groupId.add(userGroup.id);
List<GroupMember> groupMembers = [
SELECT UserOrGroupId
FROM GroupMember
WHERE GroupId IN: groupId
];
List<CalendarView> calendarViews = new List<CalendarView>();
for (GroupMember groupMember : groupMembers) {
CalendarView calendarView = new CalendarView(
Name = 'Opportunity Close Dates',
SobjectType = 'Opportunity',
StartField = 'CloseDate',
DisplayField = 'Name',
OwnerId = groupMember.UserOrGroupId
);
calendarViews.add(calendarView);
}
insert calendarViews;
I checked and triggers are not allowed on CalendarView
and it's not listed as an object under Sharing Settings either so using a trigger to automate sharing isn't feasible and neither are sharing rules. Over time, I imagine CalendarViews become easier to work with like other objects or the Calendar app itself can become more configurable so that the users are automatically viewing certain Public or Resource Calendars.
As you discovered, with a Public Calendar (a Calendar
object where Type = Public
) you can create it and have users access it without code, however there's a step each user would need to take to add it to their Calendar app view. It's also not possible to remove a user's personal calendar as the IsActive
field is not writeable on them.