I am trying to create a chatter group via apex and assign a record to it as the group record. Now I have this code to create the Chatter group:

CollaborationGroup myGroup = new CollaborationGroup();
myGroup.Name='TEST (DAV) - SALESFORCE 2';
myGroup.CollaborationType='Private'; //can be 'Public' or 'Private'                   
insert myGroup;

This works great, it creates the group, but now I want to add a record to the group. I believe it has something to do with CollaborationGroupRecordbut I cannot seem to find code on this. I believe it should be a process where I create the chatter group as highlighted before then create a collaboration group record which is then associated with the chatter group.

Any suggestions?

  • Do you want to add members? I'm a little confused by what you mean by record.
    – tugce
    Commented Oct 1, 2015 at 14:26
  • @TugceSirin No I want to add a record to the chatter group, right above in the group where you have "Post" "File" "Link" and "More", when you click more, there is a "Add Record" link where you can add an object as part of the Group Records of that Chatter group. Now I can do that graphically by clicking etc, however I want to be able to "Add Record" using apex to that group Commented Oct 1, 2015 at 14:32

2 Answers 2


There are a couple of ways to do this. Here are some samples to attach a record with an ID of recordId.

Method 1: CollaborationGroupRecord

ColloborationGroupRecord groupRecord = new ColloborationGroupRecord();
groupRecord.RecordId = recordId;
groupRecord.CollaborationGroupId = myGroup.Id;
insert groupRecord;

Method 2: ConnectApi.ChatterGroups.addRecord()

ConnectApi.ChatterGroups.addRecord(null, myGroup.Id, recordId);

Your group isn't in a community, but if it was, you'd have to add the NetworkId for Method 1, and the communityId instead of null for Method 2.


This should help you somehow : https://c.getsatisfaction.com/dbamp/topics/sf-replicateall-failing-with-summer-15-release

See More here https://rakeshistom.wordpress.com/tag/collaborationgrouprecord/

I may be doing mistake here but - for a curiosity can you try inserting CollaborationGroupFeed and giving this records id in ColloborationGroupRecord field "RecordId"

ColloborationGroupRecord  obj = new ColloborationGroupRecord ();
insert obj;
  • could you please provide me a sample of this. Im not quite sure how to create the Feed. Commented Oct 1, 2015 at 14:38
  • 1
    The RecordId to use is the ID of the record you want to add to the group. It's not a CollaborationGroupFeed. I'll provide a couple of samples in a separate answer.
    – alouie
    Commented Oct 1, 2015 at 16:31
  • "Allow in Chatter Groups" flag needs to be set for custom object on object detail page. Commented Aug 3, 2016 at 11:30

You must log in to answer this question.

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