2

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: object calendar lightning experience

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?

1 Answer 1

2

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?

Yes, that is 100% the issue.

Hard-coding Ids is bad practice. This is because Ids are pretty much guaranteed to be different when you run in (or are deploying to) a different org. The one exception to that is when you create or refresh a full-copy sandbox (also possible in a partial-copy sandbox). In that situation, the Id(s) in the sandbox will match the Id(s) in production.

Instead of doing that, you should query the ListView object (outside of your loop, queries inside of loops are practically always a terrible idea because we're limited to 100 queries in a single, sychronous transaction). Using the developer name is generally preferred because those need to be unique across your org.

Something like Listview myListview = [SELECT Id FROM Listview WHERE DeveloperName = 'Some name here'];
Do also note that SOQL queries return a List<SObject>. Salesforce allows us to assign the result of a query to a single instance when the query returns exactly one row. Trying to do this will result in an error if your query either returns 0 rows, or 2+ rows.

4
  • Thank you for your detailed response. Just for my own clarity, are you suggesting to remove this section of code: Group userGroup = [SELECT Id FROM Group WHERE Name = 'Service' LIMIT 1]; 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
    Commented Jan 9, 2023 at 20:45
  • 1
    @CDutko Not quite, it's the ListViewFilterId = '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 with userGroup. I.e. your code should be ListViewFilterId = myListView.Id. DeveloperName (also called the "api name" in some locations" is similar to the Name (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
    Commented Jan 9, 2023 at 20:50
  • I wish it was possible to give out rep! Your help has been extraordinary. Thank you so much (:
    – CDutko
    Commented Jan 9, 2023 at 20:52
  • 1
    @CDutko Also, Salesforce Ids are base-62 (not base-64) and not hex. This question goes into more detail about Salesforce Ids. You'll also be able to upvote once you hit 15 rep yourself. Accepting an answer gives me 15 rep (and you 2 rep).
    – Derek F
    Commented Jan 9, 2023 at 20:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .