A blog about Salesforce CRM Configuration and development

Saturday, 6 October 2018

How to get Report data in Apex

Sometimes we need to get the report data in Apex to present it on custom report charts. We can get the report data with the help of 'Reports.ReportResults' and 'Reports.ReportManager' class of Apex.
ReportResults: This Apex class contains the results of running a report.
ReportManager: With the help of this Apex class we can run a report synchronously or asynchronously through Apex.
 public class ReportsData{  
   public static String getreport(string reportId){    
     // Get the Report data    
     Reports.ReportResults reportReturned =Reports.ReportManager.runReport(reportId, true);    
     system.debug('Report Data: '+JSON.serialize(reportReturned));    
     //Return Report data in JSON serialize format.    
     return JSON.serialize(reportReturned);    
   }    
 }   
This line of code runs the report of specified report id and return the result.
 Reports.ReportResults reportReturned =Reports.ReportManager.runReport(reportId, true);   
Here the runReport(Id reportId, Boolean includeDetails) method takes two parameter reportId and a boolean value True or false.
 runReport(reportId, true)  
JSON is an Apex class which contains methods for serializing Object into JSON format. JSON.serialize(reportReturned) method serialize the object into JSON format.
 JSON.serialize(reportReturned)  
Calling getreport('id') method. The result (report data) will return in JSON format.
 String result = ReportsData.getreport('reportId');  
Output: Parse the result according to needs.
 {  
  "reportMetadata": {  
   "topRows": null,  
   "standardFilters": null,  
   "standardDateFilter": {  
    "startDate": null,  
    "endDate": null,  
    "durationValue": "CUSTOM",  
    "column": "CUST_CREATED_DATE"  
   },  
   "sortBy": null,  
   "showSubtotals": true,  
   "showGrandTotal": true,  
   "scope": "organization",  
   "reportType": {  
    "type": "CustomEntity$Job_Applications__c@Job_Applications__c.Positions__c",  
    "label": "Job Applications with Positions"  
   },  
   "reportFormat": "SUMMARY",  
   "reportFilters": null,  
   "reportBooleanFilter": null,  
   "name": "Job Applications by Functional Area",  
   "id": "00O28000004naGqEAI",  
   "historicalSnapshotDates": null,  
   "hasRecordCount": true,  
   "hasDetailRows": true,  
   "groupingsDown": [  
    {  
     "sortOrder": "ASCENDING",  
     "sortAggregate": null,  
     "name": "FK_Positions__c.Functional_Area__c",  
     "dateGranularity": "NONE"  
    },  
    {  
     "sortOrder": "ASCENDING",  
     "sortAggregate": null,  
     "name": "FK_NAME",  
     "dateGranularity": "NONE"  
    }  
   ],  
   "groupingsAcross": null,  
   "division": null,  
   "developerName": "Job_Applications_by_Functional_Area",  
   "detailColumns": [  
    "CUST_NAME",  
    "Job_Applications__c.Status__c"  
   ],  
   "description": null,  
   "customSummaryFormula": null,  
   "currencyCode": null,  
   "crossFilters": null,  
   "buckets": null,  
   "aggregates": [  
    "RowCount"  
   ]  
  },  
  "reportExtendedMetadata": {  
   "groupingColumnInfo": {  
    "FK_NAME": {  
     "name": "FK_NAME",  
     "label": "Positions: Position Name",  
     "groupingLevel": 1,  
     "dataType": "STRING_DATA"  
    },  
    "FK_Positions__c.Functional_Area__c": {  
     "name": "FK_Positions__c.Functional_Area__c",  
     "label": "Positions: Functional Area",  
     "groupingLevel": 0,  
     "dataType": "PICKLIST_DATA"  
    }  
   },  
   "detailColumnInfo": {  
    "Job_Applications__c.Status__c": {  
     "name": "Job_Applications__c.Status__c",  
     "label": "Status",  
     "dataType": "PICKLIST_DATA"  
    },  
    "CUST_NAME": {  
     "name": "CUST_NAME",  
     "label": "Job Applications: Job Applications Name",  
     "dataType": "STRING_DATA"  
    }  
   },  
   "aggregateColumnInfo": {  
    "RowCount": {  
     "name": "RowCount",  
     "label": "Record Count",  
     "downGroupingContext": null,  
     "dataType": "INT_DATA",  
     "acrossGroupingContext": null  
    }  
   }  
  },  
  "hasDetailRows": true,  
  "groupingsDown": {  
   "groupings": [  
    {  
     "value": "Human Resources",  
     "label": "Human Resources",  
     "key": "0",  
     "groupings": [  
      {  
       "value": "a0K28000001Bm00EAC",  
       "label": "Sr. Benefits Specialist",  
       "key": "0_0",  
       "groupings": null  
      }  
     ]  
    },  
    {  
     "value": "Information Technology",  
     "label": "Information Technology",  
     "key": "1",  
     "groupings": [  
      {  
       "value": "a0K28000001BjefEAC",  
       "label": "Software Engineer",  
       "key": "1_0",  
       "groupings": null  
      },  
      {  
       "value": "a0K28000001Bm01EAC",  
       "label": "Sr. UI Designer",  
       "key": "1_1",  
       "groupings": null  
      }  
     ]  
    },  
    {  
     "value": "Miscellaneous",  
     "label": "Miscellaneous",  
     "key": "2",  
     "groupings": [  
      {  
       "value": "a0K28000001Bl4MEAS",  
       "label": "Benefits Specialist",  
       "key": "2_0",  
       "groupings": null  
      }  
     ]  
    }  
   ]  
  },  
  "groupingsAcross": {  
   "groupings": null  
  },  
  "factMap": {  
   "1_0!T": {  
    "key": "1_0!T",  
    "aggregates": [  
     {  
      "value": 1,  
      "label": "1"  
     }  
    ],  
    "rows": [  
     {  
      "dataCells": [  
       {  
        "value": "a0O280000003iHPEAY",  
        "label": "JA-00001"  
       },  
       {  
        "value": "New",  
        "label": "New"  
       }  
      ]  
     }  
    ]  
   },  
   "2_0!T": {  
    "key": "2_0!T",  
    "aggregates": [  
     {  
      "value": 1,  
      "label": "1"  
     }  
    ],  
    "rows": [  
     {  
      "dataCells": [  
       {  
        "value": "a0O280000003ib8EAA",  
        "label": "JA-00002"  
       },  
       {  
        "value": "Review Resume",  
        "label": "Review Resume"  
       }  
      ]  
     }  
    ]  
   },  
   "1_1!T": {  
    "key": "1_1!T",  
    "aggregates": [  
     {  
      "value": 1,  
      "label": "1"  
     }  
    ],  
    "rows": [  
     {  
      "dataCells": [  
       {  
        "value": "a0O280000019ULPEA2",  
        "label": "JA-00004"  
       },  
       {  
        "value": "Hired",  
        "label": "Hired"  
       }  
      ]  
     }  
    ]  
   },  
   "T!T": {  
    "key": "T!T",  
    "aggregates": [  
     {  
      "value": 5,  
      "label": "5"  
     }  
    ],  
    "rows": null  
   },  
   "2!T": {  
    "key": "2!T",  
    "aggregates": [  
     {  
      "value": 1,  
      "label": "1"  
     }  
    ],  
    "rows": null  
   },  
   "1!T": {  
    "key": "1!T",  
    "aggregates": [  
     {  
      "value": 2,  
      "label": "2"  
     }  
    ],  
    "rows": null  
   },  
   "0_0!T": {  
    "key": "0_0!T",  
    "aggregates": [  
     {  
      "value": 2,  
      "label": "2"  
     }  
    ],  
    "rows": [  
     {  
      "dataCells": [  
       {  
        "value": "a0O280000019USBEA2",  
        "label": "JA-00005"  
       },  
       {  
        "value": "Review Resume",  
        "label": "Review Resume"  
       }  
      ]  
     },  
     {  
      "dataCells": [  
       {  
        "value": "a0O280000003jMBEAY",  
        "label": "JA-00003"  
       },  
       {  
        "value": "Phone Screen",  
        "label": "Phone Screen"  
       }  
      ]  
     }  
    ]  
   },  
   "0!T": {  
    "key": "0!T",  
    "aggregates": [  
     {  
      "value": 2,  
      "label": "2"  
     }  
    ],  
    "rows": null  
   }  
  },  
  "allData": true  
 }  
References:
ReportManager
ReportResults
Share:

0 comments:

Post a comment

Trailhead Profile


Follow by Email

Total Pageviews

Followers

Popular Posts

Powered by Blogger.

Contact form

Name

Email *

Message *