Total Pageviews

Thursday, October 9, 2014

Shortie on Analytic Report Chart API

Its a busy world and everyone wants all the important information at the click of a button, and that too in a single screen. Salesforce used dashboards  to showcase multiple information in one screen, but different people have different needs. Many people wanted charts to be combined with their normals screens hence salesforce provided "Apex Analytics API and Report Chart Component" to enable developers to build snazzy interfaces coupled with:-
1. Widgets
2. Charts (Based on reports)
3. Usual VF components

The Analytics API has been made available in Apex in the Spring ’14 release (API version 30).

We can use Analytics API along with the Visualforce component <analytics:reportChart>  to build informative and pictoral UIs.
<analytics:reportChart> had been made Generally Available (GA) in the Spring ’14 release .
 Use <analytics:reportChart> component to add Salesforce report charts to a Visualforce page. One can filter chart data to show specific results. The component is available in API version 29.0 or later.

Syntax to use report charts in VF page

<analytics:reportChart reportid="{The unique ID of the report you want to display}"  size="{tiny/small/medium/large/huge}"  showRefreshButton="{A Boolean indicating whether to add a refresh button to the chart.}" hideOnError="{Use the attribute to control whether users see a chart that has an error.}" filter="{Most Important Attribute so lets discuss this in detail}"/>



Filter Attribute of analytics:reportChart component

The attribute is used to filter a report chart by fields in addition to field filters already in the report to get specific data. 
Note that a report can have up to 20 field filters. 
A filter has these attributes in the form of a JSON string: 
  1. column: The API name of the field that you want to filter on. 
  2. operator:The API name of the condition you want to filter a field by. For example, to filter by "not equal to," use the API name "notEqual." 
  3. value: The filter criteria.

Syntax:

<analtics:reportchart reportid="" size="tiny" filter="[{column:'STAGE_NAME',operator:'equals',value:'Prospecting'}]"/>


Many times while defining the filter you may mention the API names of the fields and on running the page you will encounter "Filter exceptions".
Please note API name on the field and the API name of Column to be used in filter attribute may be different.

Ex.g. 
Column does not exist or is not filterable

To get the API name of the field and the operator, make a describe request via the Salesforce1 Reporting REST API or Salesforce1 Reporting Apex Library as shown in following examples (I used the 2nd option in developer console and got it printed):

  • Salesforce1 Reporting API {OrgURL}/services/data/v29.0/analytics/reports/00OD0000001ZbNHMA0/describe
  • Salesforce1 Reporting Apex Library                                    First,get report metadata from a describe request:      Reports.ReportManager.describeReport(00OD0000001ZbBBFFF)
I got output as follows and searched for the column name which I wanted in my case it was a field Account__c and in the output below it was displayed as "IB_Asset__c.Account__c.Id":-


DEBUG|Reports.ReportDescribeResult[reportExtendedMetadata=Reports.ReportExtendedMetadata[aggregateColumnInfo={RowCount=Reports.AggregateColumn[acrossGroupingContext=null, dataType=INT_DATA, downGroupingContext=null, label=Record Count, name=RowCount]}, detailColumnInfo={IB_Asset__c.Account__c.Name=Reports.DetailColumn[dataType=STRING_DATA, label=Account: Account Name, name=IB_Asset__c.Account__c.Name], IB_Asset__c.Japan_Modality__c=Reports.DetailColumn[dataType=STRING_DATA, label=Modality, name=IB_Asset__c.Japan_Modality__c], IB_Asset__c.Name=Reports.DetailColumn[dataType=STRING_DATA, label=IB Asset Name, name=IB_Asset__c.Name], IB_Asset__c.Product_Code__c=Reports.DetailColumn[dataType=STRING_DATA, label=Product Code, name=IB_Asset__c.Product_Code__c]}, groupingColumnInfo={IB_Asset__c.IB_Asset_Age__c=Reports.GroupingColumn[dataType=STRING_DATA, groupingLevel=0, label=IB Asset Age, name=IB_Asset__c.IB_Asset_Age__c]}], reportMetadata=Reports.ReportMetadata[aggregates=(RowCount), currencyCode=USD, detailColumns=(IB_Asset__c.Name, IB_Asset__c.Account__c.Name, IB_Asset__c.Japan_Modality__c, IB_Asset__c.Product_Code__c), developerName=IB_Asset_By_Age, groupingsAcross=null, groupingsDown=(Reports.GroupingInfo[dateGranularity=NONE, name=IB_Asset__c.IB_Asset_Age__c, sortAggregate=null, sortOrder=ASCENDING]), historicalSnapshotDates=null, id=00Of0000000PQbgEAG, name=IB Asset - By Age, reportBooleanFilter=null, reportFilters=(Reports.ReportFilter[column=IB_Asset__c.Account__c.Id, operator=equals, value=]), reportFormat=SUMMARY, reportType=Reports.ReportType[label=@IB Assets, type=IB_Assets__c]], reportTypeMetadata=Reports.ReportTypeMetadata[categories=(Reports.ReportTypeColumnCategory[columns={IB_Asset__c.Account_City__c=Reports.ReportTypeColumn[dataType=STRING_DATA, filterValues=null, filterable=true, label=Account City, name=IB_Asset__c.Account_City__c], IB_Asset__c.Account_Country__c=Reports.ReportTypeColumn[dataType=STRING_DATA, filterValues=null, filterable=true, label=Account Country, name=IB_Asset__c.Account_Country__c], IB_Asset__c.Account_Public_Private__c=Reports.ReportTypeColumn[dataType=PICKLIST_DATA, filterValues=(Reports.FilterValue[label=Yes, name=Yes], Reports.FilterValue[label=No, name=No]), filterable=true, label=Public Account, name=IB_Asset__c.Account_Public_Private__c], IB_Asset__c.Account_Region__c=Reports.ReportTypeColumn[dataType=STRING_DATA, filterValues=null, filterable=true, label=Account Region, name=IB_Asset__c.Account_Region__c], IB_Asset__c.Account_Site__c=Reports.ReportTypeColumn[dataType=STRING_DATA, filterValues=null, filterable=true, label=Account Site, name=IB_Asset__c.Account_Site__c], IB_Asset__c.Account_State__c=Reports.ReportTypeColumn[dataType=STRING_DATA, filterValues=null, filterable=true, label=Account State, name=IB_Asset__c.Account_State__c], IB_Asset__c.Account_Zone__c=Reports.ReportTypeColumn[dataType=STRING_DATA, filterValues=null, filterable=true, label=Account Zone, name=IB_Asset__c.Account_Zone__c], IB_Asset__c.Account__c.Id=Reports.ReportTypeColumn[dataType=ID_DATA, filterValues=null, filterable=true, label=Account: Account ID, name=IB_Asset__c.Account__c.Id], IB_Asset__c.Account__c.Name=Reports.ReportTypeColumn[dataType=STRING_DATA, filterValues=null, filterable=true, label=Account: Account Name, name=IB_Asset__c.Account__c.Name], IB_Asset__c.Account__c.UltimateParentId__c=Reports.ReportTypeColumn[dataType=STRING_DATA, filterValues=null, filterable=true, label=Account: UltimateParentId, name=IB_Asset__c.Account__c.UltimateParentId__c], ...}, label=IB Assets])]]

This is how I used the ReportChart tag

<analytics:reportChart reportid="00OD0000001ZbBBFFF"  size="tiny"  showRefreshButton="truehideOnError="false" filter="[{column:'IB_Asset__c.Account__c.Id',operator:'equals',value:'{!AccountId}'}]"/>