1

I have the below codes to get and run report using Reports API via apex but I need to the sort the first column of the report.

List <Report> reportList = [SELECT Id,DeveloperName FROM Report WHERE Id =: this.report];
MAP<String,String> colMap = new MAP<String,String>(); 
String reportId = (String)reportList.get(0).get('Id');

    // Run a report synchronously
        Reports.ReportResults results = Reports.ReportManager.runReport(reportId,true);
        Reports.ReportMetadata rm = results.getReportMetadata();

        // get report columns
       List<String> reportCols = rm.getDetailColumns();

// sort report column
1

There is a method to set the sort order in reporting metadata in apex

public void setSortColumn(String sortColumn)

Using the below in your code

// Run a report synchronously
    Reports.ReportResults results = Reports.ReportManager.runReport(reportId,true);
    Reports.ReportMetadata rm = results.getReportMetadata();
    Reports.SortColumn sortcolumn = new Reports.SortColumn();
    sortcolumn.setSortColumn(sortColumn);//set the sort coloumn here with the name of the column
    sortcolumn.setSortOrder(ASCENDING);
    list<Reports.SortColumn> lstcolumns = new list<Reports.SortColumn>();
    lstcolumns.add(sortcolumn);
    rm.setSortBy(lstcolumns);
  • Hi Mohith, I saw that too in the documentation but can you please share an example on how to use the method? – crusader2016 Nov 4 '16 at 4:52
  • added example see if that helps .The sortColumn there is a string – Mohith Shrivastava Nov 4 '16 at 5:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.