+ Start a Discussion
OpsterOpster 

Check in APEX if Chatter is enabled for current Org?

Does anyone know how I can check in APEX IF Chatter is enabled for the current Org?

 

I need to check either in a Visualforce page or in an Apex Class and display chatter if it is, and not display it if it is not enabled.

 

thanks

Best Answer chosen by Admin (Salesforce Developers) 
aacostaaacosta

I have yet to try and check the User field with current status because a user can clear their status, but I know this works and has been tested. Apparently, if chatter is disabled it is no longer visible to the Org/User which is why this simple code snippet works.

 

 

Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); 
if(gd.containsKey('FeedItem')){
   system.debug('Chatter is enabled!');
   return true;
}
else{
   system.debug('Chatter is disabled!');
   return false;
}

 

 

All Answers

cloudcodercloudcoder

From Winter 11, you can check

StatusCode.REQUIRED_FEATURE_MISSING

 . Here is an example from the release notes:

 

 

public void addFeedPost(String post, Id objId) 
{
  FeedPost fpost = new FeedPost(); 
  fpost.ParentId = objId; 
  fpost.Body = post;

 try
 { 
  insert fpost;
 } 
 catch (System.DmlException e) 
 { 
    for (Integer i = 0; i < e.getNumDml(); i++) 
    { 
      // Chatter not endabled in the organization // do not insert record
       System.assertEquals(StatusCode.REQUIRED_FEATURE_MISSING,  e.getDmlType(i));     
       System.Debug('Chatter not enabled in this organization:' + e.getDMLMessage());   
    }
 }
}

 

 

OpsterOpster

Can I do a check without doing an INSERT?  For example below:

 

public void checkChatterEnabled(String post, Id objId) 
	{ 
		this.chatterEnabled = true;
		
	 	try
	 	{
	 		FeedPost fpost = [SELECT id FROM FeedPost LIMIT 1];
	 	} 
	 	catch (System.DmlException e) 
	 	{ 
	    	for (Integer i = 0; i < e.getNumDml(); i++) 
	    	{
	      		// Chatter not endabled in the organization // do not insert record
	       		System.assertEquals(StatusCode.REQUIRED_FEATURE_MI​SSING,  e.getDmlType(i));     
	       		System.Debug('Chatter not enabled in this organization:' + e.getDMLMessage());
	       		this.chatterEnabled = false;  
	    	}
	 	}
	}

 

OpsterOpster

the params are not needed in that code snippet.

OpsterOpster

It looks like querying is not supported for FeedPost.  Too bad.

 

So there is no way to just check the features the org supports directly without inserting and catching the error?

cloudcodercloudcoder

As of Spring 11, you should not be working with FeedPost, rather you could query FeedItem if you limit with an id or query an EntityFeed, but you question about query vs. insert should work fine.

OpsterOpster

I am finding that this is not a good solution since if the Org does not have chatter enabled the FeedPost object is not available at all, and I cannot save the code to the org.

 

Any other ideas?

joshbirkjoshbirk

Could you do a describe on the User object to see if the CurrentStatus field exists?

b-Forceb-Force

you can try describe s-object on User object and check does there any fields exist like CurrentStatus ?

 

Thanks,

Bala

aacostaaacosta

I have yet to try and check the User field with current status because a user can clear their status, but I know this works and has been tested. Apparently, if chatter is disabled it is no longer visible to the Org/User which is why this simple code snippet works.

 

 

Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); 
if(gd.containsKey('FeedItem')){
   system.debug('Chatter is enabled!');
   return true;
}
else{
   system.debug('Chatter is disabled!');
   return false;
}

 

 

This was selected as the best answer
Salesforce SolutionsSalesforce Solutions

Just want to add this important note, which indicates that this approach will not work with a managed package:

 

"If thegetGlobalDescribemethod is called from an installed managed package, it returns sObject names and tokens for Chatter sObjects, such as NewsFeed and UserProfileFeed, even if Chatter is not enabled in the installing organization. This is not true if thegetGlobalDescribemethod is called from a class not within an installed managed package."

 

From: http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_describe_objects_understanding.htm

IbigfootIbigfoot
Have a look at the latest documentation.. this is now possible using the ConnectApi

if(ConnectApi.Organization.getSettings().features.chatter) {
   // congratulations, you have chatter!
}


AbhishekDasAbhishekDas
We can also use this

Schema.SObjectType.User.isFeedEnabled();