function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Maria SigalMaria Sigal 

Running Reports from Apex in System context when Scheduled Batch runs

I create some reports in my org . 
I wrote a batch that retrieves accounts  from reports using Report Apex API and does some calculations.
My Reports have filters , when  i run  batch from user context clicking on a link that i created  , i get right results , only filtered records but when batch runs  in system context as scheduledbatch somehow  a lot of records are retrieved as if filters have no affect , does someone have  an idea how i can get only filtered results  when batch runs from system context
results = Reports.ReportManager.runReport(currentHeatScoreSet.Report_Id__c, true);
	  	System.debug(' HeatScoreCalc=>start ' + results);
	 	Reports.ReportDescribeResult describe = Reports.ReportManager.describeReport(currentHeatScoreSet.Report_Id__c);
        Reports.ReportMetadata reportMd = describe.getReportMetadata();
        Reports.StandardDateFilter standardDateFilter = reportMd.getStandardDateFilter();
        Reports.ReportFactWithDetails factDetails;
         	factDetails =(Reports.ReportFactWithDetails) results.getFactMap().get('T!T');
        }catch(Exception exp){
             exp.setMessage('HeatScoreCalc=> HeatScoreCalc:error retrieving fact map\n' +exp.getMessage());
            Log.writeError(exp, true);
        System.debug('fact details ' + factDetails);
        Reports.ReportMetadata metaData;
        	metaData= results.getReportMetadata();
        }catch(Exception exp){
            exp.setMessage('HeatScoreCalc=> HeatScoreCalc:error retrieving metadate\n' +exp.getMessage());
            Log.writeError(exp, true);
        System.debug('HeatScoreCalc=>metadata '+ metaData);
         LIST<String> detailsColumnsAPINames = metaData.getDetailColumns();
        Integer index=0;
      	Boolean found = false;
        for(Integer i=0;i<detailsColumnsAPINames.size();i++){
            if(detailsColumnsAPINames.get(i).equals( currentHeatScoreSet.target_Id_field__c)){
                found= true;
        System.debug('HeatScoreCalc=> column number ' + index);
        objectIds = new List<String>();
            for(Reports.ReportDetailRow row: factDetails.getRows()){
                List<Reports.ReportDataCell> cells =row.getDataCells();
                if(cells!=null && cells.size()!=0){
                    Reports.ReportDataCell cell = cells.get(index);
                    objectIds.add((String) cell.getValue());
          	 HeatScoreException exp = new HeatScoreException();
             exp.setMessage('HeatScoreCalc=>HeatScoreCalc:Cant find in the report with id  ' +currentHeatScoreSet.Report_Id__c+' field id name ' + currentHeatScoreSet.target_Id_field__c);
            Log.writeError(exp, true);
            System.debug('HeatScoreCalc=>unrecognizable field  ' + found);

Kanwalpreet SinghKanwalpreet Singh
Hi Maria Sigal,
When you run report through Apex it donot count the filters which already applied to the report. To get the filter data the method needs to be used is :
Reports.ReportMetadata reportMetaDataObj = new Reports.ReportMetadata();
List<Reports.ReportFilter> reportFilters = new List<Reports.ReportFilter>();

//adding filters to run
Reports.ReportFilter reportFilterObj1 = new Reports.ReportFilter();

runReport(reportId, reportMetaDataObj , includeDetails)

Hope this will help.

Mudgala Akshay 3Mudgala Akshay 3
Thank you for your answer.
I have folowing questions.
Can you explain if I have to execute the report 2 times :
- First for retrieve filters
- Second to get the records according to the filters

If not, how can I have the filters corresponding to the selected report ?
In our case, the current user selects a report to execute it in a lightning component.
That’s why we have to know exactly which filters must be applied according to the selected report.