7

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 ???

9

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;
}
5
  • The line... sObject fItem = (sObject)Type.forName('FeedItem').newInstance(); Is giving Error: Compile Error: Method does not exist or incorrect signature: [String].forName(String) at line 381 column 38 – AngiB Oct 15 '12 at 19:26
  • Are you doing this on the IDE or on the web interface ? If on the IDE, you might need to change the API Version in the Metadata file to 26 if you're on Winter'13. – techtrekker Oct 15 '12 at 19:35
  • 3
    It sounds like you have a string variable named type in scope at this point. Try explicitly using the System namespace: System.Type.forName('FeedItem') – ca_peterson Oct 15 '12 at 21:45
  • Thank you techtrekker and ca_peterson - between your answers/comments the problem is solved. I changed the above (sObject)Type.forname to be (sObject)System.Type.Forname and I am getting chatter posts but also able to disable Chatter for the entire Org !!! – AngiB Oct 16 '12 at 2:27
  • Brilliant! That's great news! – techtrekker Oct 16 '12 at 2:30
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.