ABSYZ ABSYZ

  • Home

    Welcome to ABSYZ

  • About us

    Who We Are

  • Our Expertise

    What We Do

  • Our Approach

    How We Do It

  • Products

    What We Made

  • Industries

    Who We Do It For

  • Clients

    Whom We Did It For.

  • Article & Blogs

    What Experts Think

  • Careers

    Join The Team

  • Get In Touch

    Let’s Get Started

ABSYZ

Dynamically Changing Filters in Reports using Analytical API

Home / Article & Blogs / Apex / Dynamically Changing Filters in Reports using Analytical API

Dynamically Changing Filters in Reports using Analytical API

By satish lokin inApex, Integration, REST, Salesforce
Scenario :-

Let us Assume there are 3  representatives  working under the same territory . Now based on the selection of each representative in VisualForce page , his data should be displayed in the report  .

The Reports and Dashboards REST API gives you programmatic access to your report and dashboard data and to visualize the  data.

The Analytics API has been made available in Apex in the Spring ’14 release (API version 30).  and available for use in pages with an API version of 29 and above.

Here, in this case, we would use Analytical API to access the reports and make some changes(Update the report)  in report names ,filters ,folder etc.. for above scenario whenever the representative is changed ,we will change the filter value as that selected representative and  save changes to a report through  HTTP callouts  by sending a PATCH request to the Report resource.

This PATCH request /services/data/v29.0/analytics/reports/****000000JgMf to the Report resource ,updates and saves the report.

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:

  • column: The API name of the field that you want to filter on.
  • 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.”
  • value: The filter criteria.

{column:’ OWNER ’,

operator:’ equals ’,

value:’ Representative Name ’}

While using the Reports and Dashboards, REST API with a POST request body, we  must use content-type: application/JSON. We might get unexpected results if we don’t use this content type.

Check the below sample code :
  Callout for getting report data

[sourcecode language=”java”]
Httprequest req= new HttpRequest();
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()+
‘/services/data/v29.0/analytics/reports/*****000000JgMf/’);
req.setMethod(‘GET’);
req.setHeader(‘Content-Type’,’application/json’);
req.setHeader(‘Authorization’,’Bearer’+UserInfo.getSessionID());
Http httpReq = new Http();
HttpResponse res =httpReq.send(req);
string body =res.getBody();
[/sourcecode]

Callout for updating the Report:

 

[sourcecode language=”java”]
Httprequest reqt =new HttpRequest();
reqt.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()+’/services
/data/v34.0/analytics/reports/*****000000JgMf?_HttpMethod=PATCH’);
reqt.setMethod(‘POST’);
reqt.setbody(newbody);
reqt.setHeader(‘Content-Type’,’application/json’);
reqt.setHeader(‘Authorization’,’Bearer ‘+UserInfo.getSessionID());
Http httpReq2= new Http();
HttpResponse ress=httpReq2.send(reqt);
[/sourcecode]

Before making any callouts please add Endpoint URL in Remote site settings, please check Setup->Security->Remote site settings

Select Satish lokin from the list of Representatives and now refresh the report to view the changes in Account owner filter value.

VF Page

 

screen-shot-2016-09-29-at-23-47-58

Check the Logic for the above example

[sourcecode language=”java”]
public with sharing class DynamicController {

public String RepName { get; set; }
public PageReference changeOwer() {
//Getting the report data
Httprequest req= new HttpRequest();
req.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()+’/services/data/v29.0/analytics/reports/00O90000009BCxY/’);
req.setMethod(‘GET’);
req.setHeader(‘Content-Type’,’application/json’);
req.setHeader(‘Authorization’,’Bearer ‘+UserInfo.getSessionID());
Http httpReq = new Http();
HttpResponse res =httpReq.send(req);
string body =res.getBody();
system.debug(‘ReportStr’+body);

//The Logic will be changed based on te requirement
body = body.subStringBetween(‘”reportMetadata”‘, ‘}}’);
string subString = body.subStringBetween(‘”column”:”USERS.NAME”,’, ‘}’);
System.debug(‘Chek’+subString);
string newBody = ‘”operator”:”equals”,”value”:”‘+RepName+'”‘;
string LatestBody = body.replace(subString, newBody);
string PostBody = ‘{“reportMetadata”‘+LatestBody+’}}}’;
System.debug(‘PostBody’+PostBody);
//Updating the Report
Httprequest reqt =new HttpRequest();
reqt.setEndpoint(URL.getSalesforceBaseUrl().toExternalForm()+’/services/data/v34.0/analytics/reports/00O90000009BCxY?_HttpMethod=PATCH’);
reqt.setMethod(‘POST’);
reqt.setbody(PostBody);
reqt.setHeader(‘Content-Type’,’application/json’);
reqt.setHeader(‘Authorization’,’Bearer ‘+UserInfo.getSessionID());
Http httpReq2= new Http();
HttpResponse ress=httpReq2.send(reqt);

return null;
}
}
[/sourcecode]

[sourcecode language=”HTML”]
<apex:page controller=”DynamicController”>
<apex:form >
<h> Select any Representative</h>
<apex:selectList multiselect=”false” value=”{!RepName}” size=”1″>
<apex:actionSupport event=”onchange” action=”{!changeOwer}”/>
<apex:selectOption itemLabel=”–None–” itemValue=”–None–“/>
<apex:selectOption itemLabel=”Alex” itemValue=”Alex”/>
<apex:selectOption itemLabel=”Shar” itemValue=”Shar”/>
<apex:selectOption itemLabel=”Satish Lokin” itemValue=”Satish lokin”/>
</apex:selectList>
</apex:form>
</apex:page>
[/sourcecode]

All limits that apply to reports created in the report builder also apply to the API,  For more information, see “Salesforce Reports and Dashboards Limits”

Related links:

View Code in GitHub

Analytical API Documentation

37
Unlike this post
1 Post
satish lokin

Search Posts

Archives

Categories

Recent posts

Service Cloud for customer delight

Service Cloud for customer delight

Salesforce Financial Services Cloud

Salesforce Financial Services Cloud

Salesforce Field Service Lightning

Salesforce Field Service Lightning

Connecting the dots with Customer 360

Connecting the dots with Customer 360

Customer delight with Commerce Cloud

Customer delight with Commerce Cloud

  • Previous PostImplementation of Gantt Chart using Google Charts
  • Next PostWhat is Enhanced Email ?

Related Posts

REST API call from Einstein Analytics Dashboard
Apex REST Salesforce Salesforce Einstein Wave Analytics

REST API call from Einstein Analytics Dashboard

Create/Update Salesforce Picklist definitions using metadata API
Integration Metadata API Salesforce

Create/Update Salesforce Picklist definitions using metadata API

2 Comments

  1. Sathya
    Reply
    30 September 2016

    Had a great experience with the blog of about dynamically changing Filters in Reports using Analytical API. Thank you for sharing the blog. I feel more useful with this blog, since I am working in the same Salefoce Field.

    Reply
    • Pradeep Chary
      Reply
      30 September 2016

      Thank you for the comment and we are glad that it was helpful to you. Please follow us, we will be posting more such information covering different topics. 🙂

      Reply

Leave a Reply (Cancel reply)

Your email address will not be published. Required fields are marked *

*
*

ABSYZ Logo
  • Home
  • About us
  • Article & Blogs
  • Careers
  • Get In Touch
  • Our Expertise
  • Our Approach
  • Products
  • Industries
  • Clients
  • White Papers

ABSYZ Software Consulting Pvt. Ltd.
USA: 49197 Wixom Tech Dr, Wixom, MI 48393, USA
M: +1.415.364.8055

India: 6th Floor, SS Techpark, PSR Prime, DLF Cyber City, Gachibowli, Hyderabad, Telangana – 500032
M: +91 79979 66174

Copyright ©2020 Absyz Inc. All Rights Reserved.

youngsoft
Copy