+ Start a Discussion
solarcookersolarcooker 

Analytics API via Apex: MetadataException

Hello everybody,

I would like to run a report (Matrix report with lookup and formula fields) from Apex with the code below:

// Get the report ID
List <Report> reportList = [SELECT Id,DeveloperName FROM Report where 
    DeveloperName = 'My_custom_report'];
String reportId = (String)reportList.get(0).get('Id');

// Run a report synchronously
Reports.reportResults results = Reports.ReportManager.runReport(reportId);

The following exception is thrown:

reports.MetadataException: Can't run the report because it doesn't have any columns selected. Be sure to add fields as columns to the report through the user interface.

Can somebody explain me what the problem is?

Cheers
SravsSravs
Hi, 

The exception could happen, If all the coloumns in the report are not visable to the user. 

Regards,
Sravs


solarcookersolarcooker
Thank you Sravs for your reply. I’m running the code as administrator, so that should not be the problem.
ArunaAruna
Hi there ,

how did you resolved this issue . I am also getting same error when I am trying find the filter on the report.
Please let me know what is the solution .

List <Report> reportList = [SELECT Id,Name,DeveloperName FROM Report ORDER BY Name DESC];
     for(Integer i=0;i<reportList.size();i++){
         
         String reportId = (String)reportList.get(i).get('Id');
         String Name = (String)reportList.get(i).get('Name');
         
         Reports.ReportResults results = Reports.ReportManager.runReport(reportId);
         Reports.ReportMetadata rm = results.getReportMetadata();
         
        system.debug('---Name---'+Name);
         system.debug('---reportId---'+reportId);
         system.debug('---results'+results);
         system.debug('---rm'+rm);
          
         for(Reports.ReportFilter rf : rm.getreportFilters()){
             if(rf!=null){
                 system.debug('---col name---'+rf.getcolumn());
                 system.debug('---col value---'+rf.getValue());
             }
         }
 
solarcookersolarcooker
Hi Aruna,

As you can see this issue is not yet solved until now.
We abandoned the idea to run report from apex and schedule them instead (https://help.salesforce.com/HTViewHelpDoc?id=reports_schedule.htm&language=en_US).

Sorry if we can't help you.
Regards
 
ArunaAruna
Thank you for your reply.

is salesforce not allowing to run the reports using apex ?

Thank you,
Aruna.
solarcookersolarcooker
Hello,

I think it is possible to run report from apex according to the developper guide but using the UI is enough for us and we avoided this idea.

Cheers!