function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Lee KerfootLee Kerfoot 

Running reports to populate a custom please

How do I run a report, then populate a custom object with the report results?

The fields in the report match the custom object fields...I just don't know how to push the data from the report to the object. 
Baz DensonBaz Denson

Use Apex: You can use Apex to run a report and then loop through the results, inserting each record into your custom object. Here is an example of how you might do this:


// Run the report and get the results
ReportResult result = [SELECT Name, Email, Phone FROM Contact WHERE LastName = 'Smith'];

// Loop through the report results and create a custom object record for each record
for (ReportFactWithSummary fact : result.getFactMap().values()) {
  for (ReportRow row : fact.getRows()) {
    CustomObject__c obj = new CustomObject__c();
    obj.Name = row.getDataCells()[0].getValue();
    obj.Email = row.getDataCells()[1].getValue();
    obj.Phone = row.getDataCells()[2].getValue();
    insert obj;