SALESFORCE HOW TO CREATE CHATTER GROUPS IN APEX ?

Creating Chatter Groups in Apex

  • To create chatter groups in Salesforce dynamically we will create a chatter group first and then we'll add group members into it.
  • So let's get started by creating chatter group first.
=============================================
 CollaborationGroup myGroup = new CollaborationGroup();
 myGroup.Name='My Group'; // Define group name here
 myGroup.CollaborationType='Public'; //It can be 'Public' or 'Private'                   
 insert myGroup; 
=============================================
  • Now let,s add chatter group members into it.
=============================================
CollaborationGroupMember groupMember = new CollaborationGroupMember();
 groupMember.memberid = 'userId'; //Provide userId here
 groupMember.CollaborationGroupId = myGroup.Id; //Id of group created above
 insert groupMember;
=============================================
  • Now let's combine and create group with group member.
=============================================
 public class ApexGroup {
   public static void group(){
 CollaborationGroup myGroup = new CollaborationGroup();
 myGroup.Name='My Group'// Define group name here
 myGroup.CollaborationType='Public'//It can be 'Public' or 'Private'  
insert myGroup;
        
CollaborationGroupMember groupMember = new CollaborationGroupMember();
groupMember.memberid = 'userId'//Provide userId here
groupMember.CollaborationGroupId = myGroup.Id; //Id of group created above
insert groupMember;
   }
} 
=============================================

NOTE:
  • In case of a community or portal user you cannot access external user Id and create group between a portal user and internal user by using apex otherwise, you will get "insufficient access rights on cross-reference id" error.

If you like this salesforcekid learning platform please let me know in the Comment section...Also, Share with your salesforce folks wish you 
Happy learning ☁️⚡️ (Learn. Help. Share.)


SALESFORCE HOW TO CREATE CHATTER GROUPS IN APEX ? SALESFORCE HOW TO CREATE CHATTER GROUPS IN APEX ? Reviewed by on Rating: 5

2 comments:

  1. Can you please share the code to create Chatter groups for communities?

    ReplyDelete
    Replies
    1. Hi,
      Practically it's not possible to create group for community user with apex code. Because you are trying to create the group between the community users (External User).
      That can be only achieved with manual group creation. You can search about it !! but you will come to the same conclusion.

      Thanks

      Delete

HELP !! SHARE !! SUGGEST !!

Powered by Blogger.