salesforcesalesforce-lightningsalesforce-chatter

Chatter Api Access to create Feed via Platform Trigger


I am trying to Create A chatter feed using Chatter API(As I need mention user in the post) from A platform event trigger. But I am getting an error:- "Insufficient Privileges: The Connect API is not enabled for this user type." in Debug Logs

and the user that's shown on debug log is Automated Process and not the Authenticated user creating a post request.

So my question is Do I need to Authenticate to Chatter API inside the trigger? If yes How I can do that.

Or am I missing any chatter Configuration?

Trigger Code

trigger SampleEventsTrigger on Sample_Events__e (after insert) {
System.debug('Event Log');

for (Sample_Events__e event : Trigger.New) {
    System.debug('Event: ' + event);
    //postFeedForSmartwinnr.PostFeedMethod(event.CreatedById, event.userId__c, event.Notification_message__c); // Call function to Create Chatter
    ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
    ConnectApi.MentionSegmentInput mentionSegmentInput = new ConnectApi.MentionSegmentInput();
    ConnectApi.MessageBodyInput messageBodyInput = new ConnectApi.MessageBodyInput();
    ConnectApi.TextSegmentInput textSegmentInput = new ConnectApi.TextSegmentInput();
    System.debug('feedItemInput: ' );
    System.debug(feedItemInput);
    messageBodyInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();

    mentionSegmentInput.id = event.userId__c;
    messageBodyInput.messageSegments.add(mentionSegmentInput);

    textSegmentInput.text = event.Notification_message__c;
    messageBodyInput.messageSegments.add(textSegmentInput);
    System.debug(feedItemInput);
    feedItemInput.body = messageBodyInput;
    feedItemInput.feedElementType = ConnectApi.FeedElementType.FeedItem;
    //feedItemInput.subjectId = '0F9RR0000004CPw';
    System.debug(feedItemInput);
    ConnectApi.FeedElement feedElement = ConnectApi.ChatterFeeds.postFeedElement( Network.getNetworkId(), feedItemInput); // Error is on this line 
    System.debug('feedElement');
    System.debug(feedElement);
    }
}

Thanks in Advance...


Solution

  • As per my knowledge, the user present at that point is Automated Process who is able to post feed using class but, don't have access to chatter REST API