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
Chetan KambleChetan Kamble 

How to send chatter message in apex from a user lower in role hierarchy to a user who is higher in role hierarchy

Dear Friends,


I'm trying to send the message to the users in my organisation on  chatter from Apex class.
Sample user hierarchy where I'm trying to send the message is given below:
 User 1
     |----user 2
               |----user 3
               |----user 4
                           |----user 5
                           |----user 6

 Message sending is working fine when I am sending message from higher hierarchy user to lower hierarchy user. But from a user who is lower  in hierarchy to  a user who is  higher in hieerachy, it is not working correctly. 
eg. If i want to send message from user 1 to any user . It is working correctly.
but If I want to send from user 6  ---- > to user 5 , User 4 -----> to user 3 ,2, 1. It is throwing below error.

Error occured...Insufficient Privileges: You do not have the level of access necessary to perform the operation you requested. Please contact the owner of the record or your administrator if access is necessary.....Class.ConnectApi.ChatterFeeds.postFeedElement: line 4277, column 1

Code to send the chatter message  is given at the end. I'll appreciate if you can suggest any solution to the issue. i.e 

1- If I can send message from user who is lower in hierarchy to a user who is higher in hierarchy.
2- If I can't send message to user who is higher in hierarchy, then what are the altearnatives available.

Thanks in advance for your valuable time and help :)


Chetan Kamble

// makes a simple chatter text post to the specified user from the running user 
///comnrtrller class method to send message
public static void simpleTextPost(Id userId, String postText)

     ConnectApi.FeedType feedType = ConnectApi.FeedType.UserProfile;    
      ConnectApi.MessageBodyInput messageInput = new     ConnectApi.MessageBodyInput();
        messageInput.messageSegments = new List<ConnectApi.MessageSegmentInput>();
                // add the text segment        ConnectApi.TextSegmentInput textSegment = new ConnectApi.TextSegmentInput();
        textSegment.text = postText;
        ConnectApi.FeedItemInput feedItemInput = new ConnectApi.FeedItemInput();
        feedItemInput.body = messageInput;        
        // post it
        ConnectApi.ChatterFeeds.postFeedElement(null, userId,            ConnectApi.FeedElementType.FeedItem, postText);
        //ConnectApi.ChatterFeeds.postFeedItem(null, feedType, userId, feedItemInput, null);  
 } //simpleTextPost    

///method call from apex controller class
simpleTextPost(User , myMessage);
Best Answer chosen by Chetan Kamble
Chetan KambleChetan Kamble
Thanks vineetKumar for your valueable time.
I got the solution.I changed OWD private to public /read write for user and it started working.


All Answers

Seems like the user lower in hierarchy don't have access to the parent feed item to be visible.
Can you try running your controller in a without sharing context or try sharing the parent feed manually and then trying your code?
Chetan KambleChetan Kamble
Hi vineetKumar,

Thanks for your suggestion, but i am working on packaged version (appExchange) app development. And salesforce don't allow controller in without sharing context because of security issue while testing the code. It gives error.

And i don't understand -  sharing the parent feed manually means.

I tried with this ( but i think it is about grouping)
             ConnectApi.FeedElementType.FeedItem,originalFeedElementId );
        ConnectApi.ChatterFeeds.postFeedElement(null, userId,
            ConnectApi.FeedElementType.FeedItem, postText);

in my code.
But I coudn't understand what id to pass instead of originalFeedElementId.
Instead of posting through code, can you try posting it manually and see if it related to the code or is it some sharing thing.
originalFeedElementId - To share a feed element, specify its 18-character ID.
Chetan KambleChetan Kamble
Yes I tried it manually. My role hiererchy is 

  |------Gen Manager
I tried sending message manually cook to manager and it was successfully sent. And manager could see on his profile.
I tried sending message manually waiter to manager and iit was successfully sent.And manager could see on his profile.

Issue - But when i tried sending message manually waiter to cook. It threw same error. I think issue is on same level hierarchy sharing.
Yes you are right, this is definitely the security setting (highlighted previously as well) of users on your org. There is nothing wrong with your code.
What I suspect is that you orgwide for user is set as private and Cook and Waiter are noways visible to each other.

Just to make your scenario work.
Go to User : Cook > Click on the sharing button > Add User : Waiter > Then try sending message from waiter to cook, it should get posted.

If it does, then you might have to work out some logic to share users with each other. Not sure what your org structure is and access is required for users.
Let me know if it helped you.
Chetan KambleChetan Kamble
Thanks vineetKumar for your valueable time.
I got the solution.I changed OWD private to public /read write for user and it started working.

This was selected as the best answer
Please do mark my answer as the best answer (right under the comment) if it helped you to solve your problem.
Chetan KambleChetan Kamble
Hi vineetKumar
     Sorry , I got the solution from somewhere else. Thanks for your help.
Good that you got the solution.
But i would highly recommend you to check, why the user OWD was set as private the first hand.
Perhaps some functionality or your organisation doesn't want you to share the users among each other (hope that doesn't end up breaking things :) ).
In that case you will definitely will have to go with sharing rules.

Chetan KambleChetan Kamble
Thanks for your suggestion,
         I will check, why the user OWD was set as private the first hand. And if it breaks somewhere because of owd ,I will try with sahring rules and definitely inform you.