0

I am attempting to create a CalendarView object for my users. I am basing it 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

I am basically trying to make this object calendar for all of my users, programmatically.

Object Calendar Configuration

I am unfortunately getting stuck at with creation of the actual CalendarView object with error message:

Error: Compile Error: Invalid constructor syntax, name=value pairs can only be used for SObjects: Calendarview at line 20 column 36 

Current Code block:

/*Changelog: 
*1/5/23 Intial Release
*/

public class Calendarview
{

/*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 (Date/Time)', EndField = 'Service Visit End (Date/Time)', 
           DisplayField = 'Subject', OwnerId = groupMember.UserOrGroupId);
       calendarViews.add(calendarView);
    }
    insert calendarViews;
}

}

I am a bit surprised that I get the error message. I even deleted all of my formatting and simply had a name of 'Test' and I still had the error message. Not really sure why the compiler would be upset...

1
  • I wouldn't feel too bad about this; I'm pretty sure most of us have done this at least once by accident.
    – sfdcfox
    Commented Jan 5, 2023 at 20:08

1 Answer 1

2

Since you named your class CalendarView and you're trying to use the standard sObject Schema.CalendarView (Schema is the namespace for all sObjects), the compiler presumes that new CalendarView is meant to be calling the constructor for your class, not the sObject. This isn't allowed, as only sObjects can be constructed with the field=value notation.

There's two possible solutions from this point. Option 1 is to simply rename your CalendarView class to something else, like CalendarViewUtils or something like that. Option 2 would be to change every reference of CalendarView that's meant to use the sObject to its more fully-qualified name:

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<Schema.CalendarView> calendarViews = new List<Schema.CalendarView>();
for (GroupMember groupMember : groupMembers) 
{
   Schema.CalendarView calendarView = new Schema.CalendarView(name = 'Field Service Schedule', SobjectType = 'Case', 
       StartField = 'Service Visit Start (Date/Time)', EndField = 'Service Visit End (Date/Time)', 
       DisplayField = 'Subject', OwnerId = groupMember.UserOrGroupId);
   calendarViews.add(calendarView);
}
insert calendarViews;

Note that CalendarView, being a top-level class, may affect every class and trigger in your database. For this reason, it's strongly advised that you rename the class itself.

In the future, avoid making classes that are identical to standard objects, like Task or Event, as well as avoid using standard namespaces as classes (e.g. Test or Schema). Also, Apex is mostly case-insensitive, so a class like calendarView or cAleNdArViEw would still conflict with the standard sObject.

1
  • Wow thank you so much for the detailed response SFDCFOX. You're a legend !!
    – CDutko
    Commented Jan 5, 2023 at 21:30

You must log in to answer this question.

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