You need to sign in to do that
Don't have an account?
Maria 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
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; try{ 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; try{ 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; break; } index++; } System.debug('HeatScoreCalc=> column number ' + index); objectIds = new List<String>(); if(found==true){ 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()); } } }else{ 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); }
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 :
Hope this will help.
Thanks
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.