When you get error messages with a line #, you always want to focus on why it would say that line. Is it actually that line, or a missing semicolon on the line before, etc. It's essentially the first step
In this case it's throwing it on line 7 which is this line
g.MemberId = u.id;
The next thing is the message itself
Variable does not exist: id
That seems pretty self-explanatory - so this message is calling out that id
doesn't exist which you're using specifically in u.id
.
The next question is: why does id
not exist on my variable u
? Working backwards, you have to check what is u
. In your code
list <user> u = [select id from user where IsActive = true];
So u
is a List<User>
. Looking at Lists
A list is an ordered collection of elements that are distinguished by
their indices. List elements can be of any data type—primitive types,
collections, sObjects, user-defined types, and built-in Apex types.
Which means the variable u
could contain multiple users records.
Index 0 |
Index 1 |
Index 2 |
Contact1 |
Contact2 |
Contact3 |
This should help illuminate your issue - you can't reference a field (id
) from a user record when you're not working on the specific record.
You'll want to go through your list (so you're on a user record) and then reference the user field.
However, even if you fix that, your code is not currently doing anything. What you're currently doing is:
- Query active users
- Get a CollaborationGroup
- Create an empty list of CollaborationGroupMembers
- Loop through an empty list (nothing to loop for)
- insert an empty list (nothing will happen)
If you want to create a CollaborationGroupMember
for every user for the identified group, you need to loop through the list that actually has records (users). You'll loop through each user, create a CollaborationGroupMember
record related to them, add that record to a list (to support insert all the records at once), and then insert the list.
List<User> users = [select id from user where IsActive = true];
CollaborationGroup collaborationGroup = [select id from CollaborationGroup where Name = 'Navy Chatter Group'];
//to store all the records you'll be creating and insert
List<CollaborationGroupMember> groupMembers = new List<CollaborationGroupMember>();
for(User u : users){
CollaborationGroupMember groupMember = new CollaborationGroupMember();
groupMember.MemberId = u.id;
...
groupMembers.add(groupMember);
}
insert groupMembers;
Things to watch out for:
- When you query expecting one record, it'll throw an error if no records are returned. In this case, if there's no group called exactly
Navy Chatter Group
, then it will throw
List has no rows for assignment to SObject
- Variable names - I renamed your lists to be plural (
users
vs. u
, collaborationGroup
vs. n
). Unclear variable names make code difficult to understand - especially when it's a single letter that's unrelated to what it contains.
- Depending on the context of this code, your user query could run into issues in large orgs or those with large amounts of community users. There's limits to the number of records you can return (50,000) you need to be aware of so you want to be specific to your needs (internal only, community only, or both).
SELECT Id FROM Users WHERE UserType = 'Standard'
;