用 Apex Code 抓出 Report Data 來跑迴圈

範例背景

從一個 Account Report裡面抓出所有所列出的 Account 來跑迴圈


Sample Code


 // Run a report synchronously

String ReportId = '00O4T000001rt0ZUAQ';

Reports.reportResults Results= Reports.ReportManager.runReport(ReportId, true);

Results.getAllData();

System.debug('Value of Results.getAllData()' + Results.getAllData());


//system.debug(Results.getFactMap());



// Get the fact map from the report results

Reports.ReportFactWithDetails factDetails = 

(Reports.ReportFactWithDetails)Results.getFactMap().get('0_0!T');




//create a list of report rows and populate it with the result rows from fact map

List<Reports.ReportDetailRow> reportRows = factDetails.getRows();

System.debug('Value of reportRows.size()' +reportRows.size());



List<Id> AccountIds = new List<Id>();

for(Reports.ReportDetailRow reportRow: reportRows){

    List<Reports.ReportDataCell> datacells = reportRow.getDataCells();

    Reports.ReportDataCell datacell = datacells[0];

    system.debug(datacell.getvalue());

    AccountIds.add((Id)datacell.getvalue());

}

留言

這個網誌中的熱門文章

ISC2 CC (Certified in Cybersecurity Certification) 考試心得

Salesforce證照考試心得 (Administrator and Advanced Administrator)