2

I have a trigger (onAfter insert User) that adds a newly created User (with Community profile assigned) to a public Chatter Group. For this I have used ConnectApi. The code is the following:

public void OnAfterInsert(User[] newObjects){

    Chatter_Group_Settings__c settings = Chatter_Group_Settings__c.getValues('sandbox');
    for(User u:newObjects){

        if(u.ProfileId == settings.Customer_Community_Profile_ID__c &&
           u.Add_to_Chatter_group__c == true){

               ConnectApi.GroupMember groupMember = ConnectApi.ChatterGroups.addMember('internal',settings.Chatter_Group_ID__c,u.Id);
           }
    } 
}

The problem is that, when I enable a Contact to be a Community User and thus, create the User record, I receive the following exception:

UserTrigger: execution of AfterInsert caused by: ConnectApi.ConnectApiException: Unknown user: (System Code)

I have tried Googling for similar problems, but haven't found anything. I have debugged values and seem correct (aside from the User ID, that is not committed to DB due to the exception).

Any clues on this one?

-Magandrez

2

addMember(communityId, groupId, userId)

Adds the specified user to the specified group in the specified community as a standard member. To execute this method, the context user must be the group owner or moderator.

But in your case the context user is community user so this will not help you. You can make DML on

CollabrationGroupMember

object to add user in the chatter group. it will solve your problem.

  • Ok, I get it, but creating and inserting a CollaborationGroupMember record gives a MIXED_DML_OPERATION error, so use @future and ends up with another problem I cannot resolve: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: [] Now, the user inserting records is a System Admin and set as administrator of the Chatter Group. Any ideas? – magandrez Sep 8 '15 at 12:33
  • @magandrez can you share your updated code. – Tushar Sharma Sep 8 '15 at 12:38
  • Here it is: pastebin.com/AX04e6wh – magandrez Sep 8 '15 at 12:58
  • 1
    Please check your community group Id. i think you are giving wrong Id. Is this group created in community? – Tushar Sharma Sep 8 '15 at 13:34
  • The problem was, I was using a Chatter Group ID from the standard UI, not from the community. Thanks for pointing out that. – magandrez Sep 9 '15 at 5:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.