I am a newbie to Apex programming in Salesforce so I have been basing my code off of the example code found here: https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_calendarview.htm
With slight modifications to the Start/End/Display fields, and the object type as well as filters - the CalendarView object is helpful to my organization.
I am trying to programmatically create and distribute a calendar to all of my users with these settings:

Here is the code I am using:
/*Changelog:
*1/5/23 Intial Release
*1/6/23 Modified Class name to prevent compiler error
*/
public class ServiceSchedule
{
/Setup method for creation/assignment of calendars/
public static void getCalendar()
{
Group userGroup = [SELECT Id FROM Group WHERE Name = 'Service' 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 = 'Field Service Schedule', SobjectType = 'Case',
StartField = 'Service_Visit_Start__c', EndField = 'Service_Visit_End__c',
DisplayField = 'Subject', ListViewFilterId = '00B5w00000FytcOEAR', OwnerId = groupMember.UserOrGroupId);
calendarViews.add(calendarView);
}
insert calendarViews;
}
}
When I open an 'anonymous debugging session' I run the command "ServiceSchedule.getCalendar();"
and this is the error message I get:
Line: 26, Column: 1
System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, invalid cross reference id: []
From what I can understand the error message is saying that it can't run the "insert calendarViews" command because of something wrong with the ID? I am getting the ID from the sample code in the documentation and I am not modifying it at all. Could that be my issue?
And replace it with something "new" that is a Listview instead?
I am not fimilar with that a DeveloperName is in this context. I am assuming that is the 'real' name and not the ID that is randomly generated Hex String?
– CDutko Jan 09 '23 at 20:45ListViewFilterId = '00B5w00000FytcOEAR'bit that is problematic. You'll want to query for the listview and then use that object's Id, much like you're currently doing withuserGroup. I.e. your code should beListViewFilterId = myListView.Id. DeveloperName (also called the "api name" in some locations" is similar to theName(i.e. the label), just with spaces and special characters replaced with underscores (e.g. "Non-Recurring Revenue" would be "Non_Recurring_Revenue") – Derek F Jan 09 '23 at 20:50