+ Start a Discussion

Adding new post as separate user

I am working on an app that will automatically post messages and reminders to a chatter group. I have found that if I post these automated messages under my username, my users assume that I am the one manually writing out the messages. I want to avoid this confusion.


I want to create a chatter free user to serve as the face of my automated message, much like how Salesforce creates the "Chatter Expert" user on a new org and sends out a private message to each user. I can't run System.runAs() on a non test method, but I have found this in the Feed Item documentation:


If the logged-in user has the Insert System Field Values for Chatter Feeds user permission, the create field property is available on CreatedById and CreatedDate system fields for this object. This allows the logged-in user to set these fields to the original post author and creation date upon migration instead of accepting the system field value when migrated, which would be the logged-in user and the date the migration was performed, respectively. The fields can't be updated after migration.


And this also seems to be demoed in the 13th recipe in the Chatter Code Recipe page.


So then why is this not working when I try it myself? I am logged in as a System Admin, and the "Insert System Field Values for Chatter Feeds" is checked on by default. However, I keep on getting a INSUFFICIENT_ACCESS_ON_CROSS_REFERENCE_ENTITY, insufficient access rights on cross-reference id: [] error when I try it.


// Create Chatter Free User
Profile p = [SELECT id From profile WHERE name='Chatter Free User'];
User u = new User(alias = 'test1', email='testuser@testorg.com',
            emailencodingkey='UTF-8', lastname='testtest', languagelocalekey='en_US',
            localesidkey='en_US', profileid = p.Id,
            timezonesidkey='America/Los_Angeles', username='testingauser@foobar.com');
insert u;

// Create Post as user
FeedItem post = new FeedItem();
post.parentId = [CHATTER GROUP ID];
post.body = 'This is my test post.';
post.CreatedById = u.id;
post.CreatedDate = System.now();
insert post;


Best Answer chosen by Admin (Salesforce Developers) 
Jia HuJia Hu

Your Chatter Free User must be the member of your Chatter Group first,

then to post a feed in this Group.

All Answers

Jia HuJia Hu

Your Chatter Free User must be the member of your Chatter Group first,

then to post a feed in this Group.

This was selected as the best answer

That was it! Thanks, Jia.