ABSYZ ABSYZ

  • Home

    Home

  • 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
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
139
Like this post
2 Posts
prasadvivekkothalanka

Search Posts

Archives

Categories

Recent posts

All About The OmniStudio FlexCards

All About The OmniStudio FlexCards

Boost Customer Experience With Repeater Widget in CRM Analytics

Boost Customer Experience With Repeater Widget in CRM Analytics

Enhance Insights Using Custom Tooltips In CRM Analytics

Enhance Insights Using Custom Tooltips In CRM Analytics

Net zero as a Service In CPG Industry

Net zero as a Service In CPG Industry

How Do We Import an External Library into Salesforce Lightning?

How Do We Import an External Library into Salesforce Lightning?

  • 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

Integrate SharePoint with Salesforce using Microsoft Graph API
Apex Integration Salesforce

Integrate SharePoint with Salesforce using Microsoft Graph API

Cyber-security in an uncertain world
Apex

Cyber-security in an uncertain world

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

INDIA | USA | UAE

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

Copyright ©2022 Absyz Inc. All Rights Reserved.

youngsoft
Copy
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “ACCEPT ALL”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent. Privacy Policy
Cookie SettingsREJECT ALLACCEPT ALL
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled

Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.

CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.

Functional

Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.

Performance

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

Analytics

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.

Advertisement

Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.

Others

Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.

SAVE & ACCEPT