This is in test class.

I wanted to create a collaboration group and assign it to a network.

Here's my code:

CollaborationGroup group = new CollaborationGroup();
group.Name = 'My Group';
group.CollaborationType = 'Public';
group.NetworkId = networkId;
insert group;

But when I query it using this code:

List<CollaborationGroup> groups = [SELECT Id, Name, NetworkId FROM CollaborationGroup];

It wouldn't have NetworkId on it or groups[0].NetworkId is null.

I didn't get any error.

All of this code is in the same method in test class.


I just read the documentation from Salesforce, it says "https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/sforce_api_objects_collaborationgroup.htm".


group.NetworkId = 'someid';

will not work.

I've tried passing it in the constructor like:

CollaborationGroup group = new CollaborationGroup(Name = 'My Group', CollaborationType = 'Public', NetworkId = 'someid');

but still doesn't work.

  • Is this in a test class or your working code? Are you doing this in Anonymous Apex or a class saved in your org? Is your query code part of of the same class code, different class code, test code, or are you manually querying (and if so, where)? Are you getting an errors or other response on your insert statement? Please edit your question to add more info.
    – Moonpie
    Sep 24, 2021 at 15:34
  • Hi @Moonpie, thanks for your clarification. I have already updated my question.
    – Carl Ortiz
    Sep 24, 2021 at 15:43
  • What do you get if you temporarily add a System.debug('group: ' + group); after your insert?
    – Moonpie
    Sep 24, 2021 at 15:45
  • It would put this: group: (CollaborationGroup: {Id=906F00000008wiLIAQ, Name=My Group, NetworkId=9062I000000BlpKQAS}), but when I try to query, the NetworkId is gone.
    – Carl Ortiz
    Sep 24, 2021 at 15:52
  • 1
    I've read the documentation that says "You can only add a NetworkId when creating a group. You can’t change or add a NetworkId for an existing group. This field is available in API version 26.0 and later.". So, group.NetworkId = 'somevalue'; will not work.
    – Carl Ortiz
    Sep 27, 2021 at 0:27

1 Answer 1


You need to get the NetworkId as You cannot create data for the Network as it represents a Community.

Get the profile id that is added as a member of the community(Network).

Profile p = [select id from profile where name = 'community profile name' limit 1]; 

Create a test user with this profile in the Test class and Query on NetworkMember

NetworkMember membership = [SELECT network.name, networkId, memberID FROM NetworkMember WHERE memberID = :testUser.Id limit 1];

Use the below Query to get NetworkId

Network net =[select id from Network where name ='Portal Name' limit 1];

Map in your code as below

group.NetworkId = net.id;

You must log in to answer this question.

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