• Maria Sigal
  • NEWBIE
  • 0 Points
  • Member since 2015

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
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;
        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);
        }