Find your content:

Search form

You are here

Posting to Chatter via APEX in a Managed Application - Allow Chatter to be disabled

 
Share

We are going to enhance our managed package by adding automatic chatter updates when a user takes certain actions in the application.

I found some examples in the Chatter Code Recipes that works nicely:

FeedItem post = new FeedItem();
post.ParentId = MyPostId; // User, Account, Contact or Opportunity 
post.Body = MyPostBody;
insert post;

This works like a charm. The problem that I have is I do not want to force my customers or prospective customers to have chatter enabled in their organization.

If I put the above code into a class in my development org and then I go to Setup - Customize - Chatter - Settings and try to uncheck the 'Enable' to turn on chatter I get a message:

Cannot disable Chatter. It is referenced by the following components: MyApexClassName

Based on more reading I tried the following as supposedly this checks if Chatter is enabled in the org:

if (Schema.SObjectType.User.isFeedEnabled())
{
  FeedItem post = new FeedItem();
  post.ParentId = MyPostId; // User, Account, Contact or Opportunity 
  post.Body = MyPostBody;
  insert post;
}

But still if I try to disable Chatter in the org it gives me the same error message.

Is there a way to write my Apex chatter feed posting such that I don't preclude someone running my Managed Application from turning off chatter ???


Attribution to: AngiB

Possible Suggestion/Solution #1

Creating Dynamic SObject's is certainly the safest way to avoid Chatter being a requirement to your managed package. It means you can disable Chatter in your packaging org and be certain there are no requirements by the managed package.

However to slightly contradict what @techtrekker has said. It is possible to have compile time references to Chatter entities in your managed package and still retain Chatter as an optional requirement.

The managed package that my organisation sells, has plenty of code referencing the FeedItem object individual *__Feed objects, and we even have triggers on the FeedItem and FeedComment objects. The important thing is to make sure the Chatter requirement is unchecked when uploading the managed package (see the image below).

An important gotcha that prompted me to write this answer. References to anything Chatter related in anything but Apex Classes/Triggers and Visualforce Pages/Components will force Chatter to be a requirement of your managed package. I discovered this because we recently added a fieldset to the User object, and one of the available fields was the "About Me" field, turns out this field is only available on the User object when Chatter is enabled, and once we removed it from the fieldset Chatter was no longer a requirement of our managed package.

Chatter not a Package Requirement


Attribution to: Daniel Blackhall

Possible Suggestion/Solution #2

You would need to use Reflection so as to not have any compile time reference to chatter entities. So

if (Schema.SObjectType.User.isFeedEnabled())
{
sObject fItem = (sObject)System.Type.forName('FeedItem').newInstance();
fitem.put('ParentId' ,UserInfo.getUserId()); // or Account or Contact or Opportunity Id
fItem.put('Body' ,'Hello world');
insert fItem;
}

Attribution to: techtrekker
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/3339

My Block Status

My Block Content