-2

I am trying to add all active users in our org to a specific chatter group using the anonymous window, but I get this error message saying

Line: 7, Column: 20 Variable does not exist: id" not sure what I am doing wrong.

Any suggestions please?

list <user> u = [select id from user where IsActive = true];

CollaborationGroup n = [select id from CollaborationGroup where Name = 'Navy Chatter Group'];

list <CollaborationGroupMember> groupMembers = new list <CollaborationGroupMember>();
for (CollaborationGroupMember g : groupMembers){
    g.MemberId = u.id; 
    g.CollaborationGroupId = n.id;
    groupMembers.add(g);
}
insert groupMembers;

2 Answers 2

3

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';

1
  • 2
    Thank you for all those suggestions. I am new to the programing and hungry to learn from great people like yourself. I appreciated all those recommendations and will definitely add them to my tool box.
    – Alex
    Commented Feb 25, 2022 at 20:52
0

This works perfect... without cross reference error

trigger addMemGroup on User (after insert) {
List<User> users = [SELECT Id FROM User WHERE IsActive = true ORDER BY CreatedDate DESC LIMIT 1];
CollaborationGroup collaborationGroup = [SELECT Id FROM CollaborationGroup WHERE Name = 'Support Group test' LIMIT 1];

if(collaborationGroup != null){
    List<CollaborationGroupMember> groupMembers = new List<CollaborationGroupMember>();

    for(User u : users){
        CollaborationGroupMember groupMember = new CollaborationGroupMember();
        groupMember.MemberId = u.Id;
        groupMember.CollaborationGroupId = collaborationGroup.Id;
        groupMembers.add(groupMember);
    }

    insert groupMembers;
}

}

You must log in to answer this question.

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