• External Community
  • Chatter Group in External Community
  • Apex Email Service trying to insert FeedItem related to Chatter Group

Sample Code

FeedItem fi = new FeedItem(
    parentId = 'chatter group id in external community',
    body = 'Hello World'

insert fi;

The above code snippet fails when ran from Apex Email Service or Developer Console with error:

System.DmlException: Insert failed. First exception on row 0; first error: INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: [].

If I change the parentId to be 'chatter group id in internal community' then it works beautifully. But when it's a group that belongs to an external community then error.

I know that in Apex there is the ConnectApi and it has various postFeedElement( communityId, ... ) methods that let you specify the CommunityId. But my requirement is that I need to set the FeedItem.CreatedById to be someone other than the context user for my open source project Chatter Bot for Feeds.

The ConnectApi does not let you specify the author it instead defaults to the context user. Hence why I'm trying to do this with FeedItem and DML directly via a context user with "Insert System Field Values for Chatter Feeds" permission so that I can set the author of the post to whomever I want.

This concept works great in the internal community, but failing when the apex code tries to insert FeedItem whose parent is a group that belongs to a external community.

1 Answer 1


Ok, figured it out thanks to Neil Hayek of Salesforce. Answer is super simple!

Set the community id in the FeedItem.NetworkScope field.

FeedItem fi = new FeedItem(
    parentId = 'chatter group id in external community',
    body = 'Hello World',
    networkScope = '<community id>' // e.g. 0DB0a000000PBDHGA4

insert fi;

I completely missed this and was looking for NetworkId field which doesn't exist. Doh!


enter image description here

  • 1
    Ah I never clicked into this question and just assumed you were already setting this field.
    – Adrian Larson
    Mar 28, 2017 at 16:57
  • I appreciate you thought I was smart enough to have known about that field and to have found it. I feel uber sheepish on this miss. But glad its solved!
    – Doug Ayers
    Mar 28, 2017 at 16:59
  • The SOAP API Developer Guide is your friend. :)
    – Adrian Larson
    Mar 28, 2017 at 17:00

You must log in to answer this question.

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