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

How to schedule a report from an apex class

Home / Article & Blogs / Apex / How to schedule a report from an apex class

How to schedule a report from an apex class

By prasadvivekkothalanka inApex, REST

In this blog, I am going to take you through the process of how to schedule a report from apex class. While I was working on my last project I came across a scenario where I need to schedule a report from the apex. This blog takes you through the simplest way possible to achieve this requirement.

Using analytics REST API we can do the following things :

  1. Create, update and retrieve the Salesforce Report and Dashboard.
  2. Modify and schedule trends in Analytics report snapshot.
  3. Retrieve a list of dataset versions.
  4. Retrieve a list of dependencies for an application.

In order to schedule a report first, we need to understand ‘reports’ namespace.

Reports namespace contain a number of classes through which we can avail the same level of data access as in case of using Salesforce Reports and DashBoards REST API.

There are two ways of running a report through apex.

  1. Synchronously
  2. Asynchronously

Synchronous Way of running  reports:

If you want to run reports synchronously we have to use ReportManager class. In this, we need to use runreport() Method. The Return type of this report must be ReportResult class. This class contains results after running a report.

Check the below code for running report synchronously.

[sourcecode language=”java”]
public class ScheduleReport {

public ScheduleReport(){

list reportList=[select id, name from report where name=’DemoReport’’];

// To get the report Id.

string reportId=(string) reportList.get(0).get(‘Id’);

//To Run the report.

reports.ReportResults reportResult=reports.ReportManager.runReport(reportId, true);

system.debug(‘reportResult—>’+reportResult);

}

}
[/sourcecode]

Asynchronously way of running reports:

If you want to run reports asynchronously we have to use ReportManager class similar to Synchronous way of running reports. Here you need to use runAsyncReport() method instead of runReport() method.

Advantages of running reports Asynchronously:

  1. We can lower the risk of reaching the timeout limit when we run reports.
  2. Salesforce API timeout limit does not apply for asynchronous runs.
  3. Salesforce Reports and dashboards API can handle higher number of asynchronous run requests at a time.

Check the below code to Schedule a report Asynchronously

[sourcecode language=”java”]
public class ScheduleReport {

public ScheduleReport(){

list reportList=[select id, name from report where name=’DemoReport’’];

string reportId=(string) reportList.get(0).get(‘Id’);

Reports.ReportInstance instance = Reports.ReportManager.runAsyncReport(reportId, true);

System.debug(‘Asynchronous instance: ‘ + instance);

}

}
[/sourcecode]

Now we need to write a schedulable class to schedule our report.

Below is the code for schedule class.

[sourcecode language=”java”]
global class ScheduleReportClass implements Schedulable{

global void Execute(SchedulableContext context){

ScheduleReport sch=new ScheduleReport();

}

}
[/sourcecode]

Let us consider a scenario where I need to schedule a report every weekday at 1PM.

Below is the code for scheduling

[sourcecode language=”java”]
ScheduleReportClass sch=new ScheduleReportClass();
string cronExp=’0 0 13 ? * MON-FRI’;
system.schedule(‘schedule report’, cronExp, sch);

[/sourcecode]

analytics REST APIReportsschedule report
40
Unlike this post
2 Posts
prasadvivekkothalanka

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 PostDREAMFORCE 2018: TOP 5 Sessions You Cannot Afford to Miss if you are in Sales/ops
  • Next PostConfiguring Live Agent in Salesforce: Deployment, Chat Buttons in Visualforce

Related Posts

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

REST API call from Einstein Analytics Dashboard

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