function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
dev401hasdev401has 

Insufficient access rights on cross-reference id for Chatter group feed post

Hi

 

I have created a chatter group (Collaboration type is public). I am trying to post a feed into that using a trigger

 

the trigger goes like this:

 

trigger postfeed on  custom_object__c(after insert) {

    for(custom_object__c c: trigger.new)
    {
       try
       {
            FeedPost fpost = new FeedPost();
         // fpost.Type = 'TextPost';
            fpost.ParentId = chattergroup.Id;
            fpost.Body = 'Feedpost to be posted in chatter group';
            insert fpost;
       }

    }

}

 

 

and it works for system admin (owner of that chatter group).

 

but when i log in using another user having different profile (the profile has access to that custom object and user is member of that Chatter group) then it gives following error:

 

 

04:20:11.963|DML_BEGIN|[25]|Op:Insert|Type:FeedPost|Rows:1
04:20:12.625|DML_END|[25]
04:20:12.625|EXCEPTION_THROWN|[25]|System.DmlException: Insert failed. First exception on row 1; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: []
04:20:12.626|METHOD_ENTRY|[29]|ApexPages.addMessages(APEX_OBJECT)
04:20:12.626|VF_PAGE_MESSAGE|Insufficient Privileges
04:20:12.626|METHOD_EXIT|[29]|ApexPages.addMessages(APEX_OBJECT)

 

 

Can anyone help me out on how to get rid of this access error?

Is there anything like we need to give access to chatter group for some profiles???

 

Best Answer chosen by Admin (Salesforce Developers) 
dev401hasdev401has

replying my post itself. Hope it helps other.

 

You can post into Chatter Groups only if you are member of that group.

All Answers

dev401hasdev401has

replying my post itself. Hope it helps other.

 

You can post into Chatter Groups only if you are member of that group.

This was selected as the best answer
ClaiborneClaiborne

A work-around is to check if the current user is a member of the group. If not, put the member into the group. This may not work for everyone, but my client finds it acceptable.

 

 

Assuming that you have already found the Chatter Group as an object chatterGroup . . .

List<CollaborationGroupMember> mlist = [
    select id
    from CollaborationGroupMember  
    where MemberId = : UserInfo.getUserId()
        and CollaborationGroupId = : chatterGroup.id];
                
if (mlist.size() == 0) {
        // Add user to group
    CollaborationGroupMember cgm = new CollaborationGroupMember(
        MemberId = userInfo.getUserID(),
        CollaborationGroupId = chatterGroup.id);
    insert cgm;
}

 

 

dev401hasdev401has

Hi David

 

Thanks for the reply.

I am also doing the similar way. Inserting the member into group, posting the feed and then deleting the member from the group.

I am doing upsert for group member into Group and then checking whether user was inserted or updated. If user was already the member then it would be updated hence no need to delete it and if not i.e. user is inserted then delete the user from group.

 

But I was not able to find anything for private group hence I had to use this workaround. If somewhere around you come across any solution for Private groups do post back.

Sure@DreamSure@Dream

Hi David, 

I have a commandbutton, "Join Group" through which the authenticated users in site can join the collaboration group.
It works for System administrators, but when i click that button by loggin in as authenticated site user, i am getting the same error--
"Insufficient access rights on cross-reference id".

Any inputs for this??
CollaborationGroupMember cgm = new CollaborationGroupMember(
        MemberId = userInfo.getUserID(),
        CollaborationGroupId = chatterGroup.id);
try
{
    insert cgm;
}
catch(DMLEXCepion e)
{
//display dml error

}
I am inserting the group member like this