I am trying to write code that will post to an objects chatter feed and mention a list of users so they will get a push notification on salesforce1. This code will be a batch which is run from a trigger when a record is inserted in to my org(via salesforce2salesforce). When a record is created this way the trigger is run under the context of "Connection User". This is a problem because "Connection User" does not have access to the Connect API. How can I change the current user(in apex) or give "Connection User" access to the Connect API?

Here is an example error message and code:

Error inserting ChatterTest record(s). Cannot insert ChatterTest (ChatterTest Name : R9). Message: ChatterTestPost: execution of AfterUpdate caused by: System.NoAccessException: Insufficient Privileges: The Connect API is not enabled for this user type. (System Code) Class.ChatterTest.EventWatchChatterSetup: line 57, column 1 Trigger.ChatterTestPost: line 3, column 1 . StatusCode = CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY.

global with sharing class ChatterTest implements Database.batchable<sObject>, Schedulable{

global ChatterTest(){

global Database.QueryLocator start(Database.batchableContext info){
    return Database.getQueryLocator(
        [select Id, SomeText__c
        from  ChatterTest__c
        where NotificationSent__c = false // get all those events where notifications haven't been sent yet
        limit 10000]); // hardcoded for testing

global void execute(SchedulableContext SC) {
    Database.executebatch(new ChatterTest());

global void execute(Database.batchableContext BC, List<ChatterTest__c> scope){
    for (ChatterTest__c anEvent : scope){
        EventWatchChatterSetup (anEvent.id, anEvent.SomeText__c);

global void finish(Database.BatchableContext BC){

private static void EventWatchChatterSetup (Id eventId, String msg) {
    // First have the users follow the Event feed
    // Use EntitySubscription:
    //EntitySubscription entSub = new EntitySubscription (parentId = new Event's Id,
    //                    subscriberid = userId);
    List<EntitySubscription> newSubs = new List<EntitySubscription>();
    newSubs.add(new EntitySubscription(parentId = eventId,
                                       subscriberid = '005j000000BaH7L'));
    Database.insert(newSubs, false);

    ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
    ConnectApi.MentionSegmentInput mentionSegmentInput = new ConnectApi.MentionSegmentInput();
    ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
    ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();

    messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();

    textSegmentInput.text = msg;

    // Mention a user
    mentionSegmentInput.id = 'XXXXXXXXXXXXX'; // replace XXXXXXXXXXXXX with your user id

    feedItemInput.body = messageBodyInput;
    feedItemInput.feedElementType = ConnectApi.FeedElementType.FeedItem;

    // Use a record ID for the subject ID.
    feedItemInput.subjectId = eventId;

    ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement(null, feedItemInput, null);        

1 Answer 1


Short answer - I don't think you can. I've had similar problems when trying to get a guest site user to write to Chatter.

If you are running a batch, you could have to have some scheduler that starts a batch every few minutes - and have the batch kick off a new one if there are records to process - that way, the batch would run in the right context. Dan Appleman has done some good presentations on how to create that sort of continuos loop.

As a side note - if you are bulk adding FeedItem records, I would suggest using the new BatchInput feature in the Connect API to avoid DML limits

You must log in to answer this question.

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