Actionable Analytics using Chatter  

Less than 30 lines of code!

Arun V
Salesforce Analytics
3 min readOct 28, 2013

--

Most often users have to open the report in one tab, Chatter in another and then go through the manual process of navigating to their Chatter page to post the message. With the advent of the Analytics API, we can easily build a simple solution to solve this use-case and that too in under 30 lines of code.

Step1: Let’s start with the Source Report

For this example, we will use the report that gives us the names of case owners and the number of open cases. We will use this report as a source and make chatter posts to the respective case owners.

Step2: Running a report using APEX

In order to make a HTTP callout to the Analytics API REST endpoint we should open the access to the remote site. You can do it under the “Remote Site” section in Setup.

Once the callout access can be enabled, the actual code to run a report from APEX is just 5 lines.

// Run Report using Analytics API
HttpRequest req = new HttpRequest();
req.setEndpoint(‘https://cs12.salesforce.com/services/data/v29.0/analytics/reports/00OV0000000OTyR’);
req.setHeader(‘Authorization’, ‘Bearer ‘+ UserInfo.getSessionId());
req.setMethod(‘GET’);
Http http = new Http();
HTTPResponse res = http.send(req);

Step3: Get the list of case owners

The Analytics API uses JSON for both request and response. In order to make it easy to interact with JSON response we will be using this simple wrapper object — JSObject — https://github.com/arun-sfdc/Analytics-API/blob/master/JSObject.apex

JSObject reportResult = JSObject.wrap(JSON.deserializeUntyped(res.getBody()));

reportResult.getAsObject(‘groupingsDown’).getAsList(‘groupings’)

Step4: Make a Chatter post of every user

Once we have the report results in APEX, the process of getting the list of case owners and the number of open cases is fairly straight-forward. Also, since Chatter is natively available in APEX, the actual integration code is less than 10 lines.

// Make a feed post for each user in the report
List<FeedItem> posts = new List<FeedItem>();
for(JSObject user: reportResult.getAsObject(‘groupingsDown’).getAsList(‘groupings’)) {

String factMapKey = user.get(‘key’)+’!T’; // Refer to “Decode the FactMap” JSObject factMapEntry = reportResult.getAsObject(‘factMap’).getAsObject(factMapKey);

String numCases = (String) factMapEntry.getAsList(‘aggregates’).get(0).get(‘label’); // Construct the Feed object

FeedItem post = new FeedItem(); post.parentId = (Id)user.get(‘value’); post.body = ‘Alert!! You have ‘+ numCases + ‘ open cases.’; posts.add(post);
}
insert posts;

If you would like to download the complete source code, please follow these GitHub links:

ReportToChatter.apex: https://github.com/arun-sfdc/Analytics-API/blob/master/reportToChatter.apex

JSObject — https://github.com/arun-sfdc/Analytics-API/blob/master/JSObject.apex

Previous Blog Posts:

D3: https://medium.com/p/c87517ab52b1

Google Maps: https://medium.com/p/13c4a1198953

--

--