0

I am attempting to create a calendar view object for my users. It is my first forray into Apex programming, so I have been referencing the example code in the developer docs.

I ran into an issue with the example code - it does not compile.

https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_calendarview.htm

This is the documentation that I am reading/following for the CalendarView object.

The code in question is here:

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;

By default, this code does not compile and I recieve error message:

Error: Compile Error: Unexpected token ')'. at line 4 column 25 

From what I can understand, this code to me is properly formatted. We create a group named userGroup. We then create a list named groupId. In the list, we are attempting to add the id from the group we just created.

Group userGroup = [SELECT Id FROM Group WHERE Name = 'Sales Group' LIMIT 1];
List<Id> groupId = new List<Id>();
groupId.add(userGroup.id);

Nothing seems out of the ordinary to me, but even with just these 3 lines and modifying the group name from 'Sales Group' to something actually real in my salesforce environment I still receive nothing but the compile error as a response.

1 Answer 1

1
CalendarView calendarView = new CalendarView(name = 'Opportunity Close
Dates', 

Is the problem, because you cannot have literal line breaks in an Apex string. If you move it up to the previous line:

CalendarView calendarView = new CalendarView(name = 'Opportunity Close Dates', 

The code will compile and run.

6
  • I don't believe that this is the case... as that section of the code is not the problem. It is the first 3 lines. All of which are on a single line: Group userGroup = [SELECT Id FROM Group WHERE Name = 'Sales Group' LIMIT 1]; List<Id> groupId = new List<Id>(); groupId.add(userGroup.id);
    – CDutko
    Commented Jan 5, 2023 at 19:09
  • public class Calendarview{ List<Id> groupId = new List<Id>(); groupId.add(1); } Even if I do an absolutely beyond simple section of code I get the same sort of error: Error: Compile Error: Extra ')', at '1'. at line 4 column 13
    – CDutko
    Commented Jan 5, 2023 at 19:13
  • 1
    @CDutko I see your problem now. In Apex, you can only declare and initialize properties outside of a method. You're trying to put executable code outside a method (groupId.add(1);), which isn't allowed. You need to put the code from the example in a method: public class MyClass { public static void myMethod() { ** code goes here** } }.
    – sfdcfox
    Commented Jan 5, 2023 at 19:17
  • that was the issue! Now I need to figure out how to use my custom "case" object in my calendar... saying now that: Error: Compile Error: Invalid constructor syntax, name=value pairs can only be used for SObjects: Calendarview at line 14 column 36
    – CDutko
    Commented Jan 5, 2023 at 19:26
  • 1
    @CDutko That's going to be more complicated. I'd suggest you take the time to open a new question, I'd be happy to take a look at it for you.
    – sfdcfox
    Commented Jan 5, 2023 at 19:36

You must log in to answer this question.

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