Activity › Forums › Salesforce® Discussions › Can report data be accessed programmatically through Salesforce Apex? › Reply To: Can report data be accessed programmatically through Salesforce Apex?
MemberJanuary 15, 2018 at 3:16 pm
You can refer to the standard salesforce api guide and search "RUN REPORTS" for this. Or you can use the below piece of code.
// Get the report ID
List <Report> reportList = [SELECT Id,DeveloperName FROM Report where
DeveloperName = 'Closed_Sales_This_Quarter'];
String reportId = (String)reportList.get(0).get('Id');
// To run a report synchronously
Reports.ReportResults results = Reports.ReportManager.runReport(reportId, true);
System.debug('Synchronous results: ' + results);
// To run a report asynchronously
Reports.ReportInstance instance = Reports.ReportManager.runAsyncReport(reportId, true);
System.debug('Asynchronous instance: ' + instance);
Hope this helps you.